简体   繁体   中英

Set all instances of a value to null in Realtime Database: Firebase Cloud Functions

I want to take wipe all of the values of a particular userID connected to many different post keys in my database by turning the userID to null. The userIDs are attached to post keys in the path: posts/ivies/userIDs in my database. Here is how the database looks:

数据库结构

So I decided to run the following for loop to filter for the userID and turn it to null:

exports.wipeData = functions.https.onRequest(async (req, res) => {
 const original = 'ppPXA8MvaSRVbmksof0ByOzTxJ92';
    const snapshot = await admin.database().ref('/posts/ivies/userIDs/');
    console.log((snapshot));

    for (let value in snapshot.val) {
      if (value == original) {
      snapshot.val.set("null")
      }
      else {
        console.log(value)
      }
    }

    res.redirect(303, snapshot.ref.toString());

// [END adminSdkPush]
});

Although this function deploys and runs, it does not turn 'ppPXA8MvaSRVbmksof0ByOzTxJ92' to 'null' as anticipated. Thank you for your help.

Your general approach seems fine, but you have a few bugs in there.

This should work better:

exports.wipeData = functions.https.onRequest(async (req, res) => {
  const original = 'ppPXA8MvaSRVbmksof0ByOzTxJ92';
  const ref = admin.database().ref('/posts/ivies/userIDs/');
  const query = ref.orderByValue().equalTo(original);

  const results = await query.once('value');
  const updates = {};
  results.forEach((snapshot) => {
    updates[snapshot.key] = null;
  });

  await ref.update(updates);

  res.status(200).send(JSON.stringify(updates));
})

The main changes:

  • Your snapshot variable doesn't contain any data yet, as you're not reading from the database. The once('value') in my code performs that read.
  • This code uses a query to select only the nodes that have the right value. When your number of users grows, this significantly reduces the database load (and cost).
  • This code first gathers all updates into a single object, and then sends them to the database as one call.
  • The await in await ref.update(updates) is probably the main fix, as it ensures the redirect is only executed once the database writes has been completed.

I am not familiar with firebase cloud functions, but in regular client-side firebase code val needs to be called as a function, and you have to wait for the value from a reference. You could try:

exports.wipeData = functions.https.onRequest(async (req, res) => {
  const original = 'ppPXA8MvaSRVbmksof0ByOzTxJ92';
  const userIDs = await admin.database().ref('/posts/ivies/userIDs/');
  userIDs.once("value", snapshot => {
    var lookup = snapshot.val();
    for (let key in lookup) {
      var value = lookup[key];
      if (key == value) {
        userIDs.child(key).set(null);
      }
    }
    res.redirect(303, userIDs.ref.toString());
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM