简体   繁体   中英

firebase cloud function not updating the data

I have firebase real-time database which has user node and contains many users I want to set rating property of user to 0, for that first I need to read the database and the update the database.

my function is reading the data but unable to update and return {} on the browser

exports.quarter = functions.https.onRequest((req, res) => {
 let user1 = admin.database().ref('users/user1/userDetails').once('value');
 let user2 = admin.database().ref('users/user2/userDetails').once('value');
 let user3 = admin.database().ref('users/user3/userDetails').once('value');
 let user4 = admin.database().ref('users/user4/userDetails').once('value');
  Promise.all([user1, user2, user3, user4])
      .then(result => {
    let data = {};
     result[0].forEach(action => {
         data['users/user1/userDetails/' + action.key + '/' + 'Rating'] = 0;
     });
     result[1].forEach(action => {
         data['users/user2/userDetails/' + action.key + '/' + 'Rating'] = 0;
     });
    result[2].forEach(action => {
        data['users/user3/userDetails/' + action.key + '/' + 'Rating'] = 0;
    });
    result[3].forEach(action => {
        data['users/user4/userDetails/' + action.key + '/' + 'Rating'] = 0;
    });
     return data;

}).then(data => {
      console.log(data);
     return  admin.database().ref().update(data);

 }).then(data => {
     return res.send('done');

 }).catch(error => {
    return res.status(500).send(error);
 })
});

I can see the result of console.log(data) but the function is not updating and finally after performing get request it's returning {} .

what is wrong with this?

this line admin.database().ref().update(data) your reference seems to be undefined , take a look at the docs

I'm not sure if you mean something like keeping the ref you defined at top

 let user1 = admin.database().ref('users/user1/userDetails').once('value');

if that is the case.. you have to something like

 let user1 = admin.database().ref('users/user1/userDetails');
  user1.once('value')

 // and down where you want to update
 user1.update.(data)

But notice this is only the data for that node imparticular, you can not update em all at once if you want to use your approach

Promise.all() is returning before any of the promises passed to it are going to finish. This means data is going to be an empty object has original defined, and that value is returned immediately. Instead you should have the following then callback handle each result:

exports.quarter = functions.https.onRequest((req, res) => {
 let user1 = admin.database().ref('users/user1/userDetails').once('value');
 let user2 = admin.database().ref('users/user2/userDetails').once('value');
 let user3 = admin.database().ref('users/user3/userDetails').once('value');
 let user4 = admin.database().ref('users/user4/userDetails').once('value');
 return Promise.all([user1, user2, user3, user4]);
}).then(data => {
  // data will be an array of all the results. Handle them here.
 })
});

The following should do the trick:

exports.quarter = functions.https.onRequest((req, res) => {
  let user1 = admin
    .database()
    .ref('users/user1/userDetails')
    .once('value');
  let user2 = admin
    .database()
    .ref('users/user2/userDetails')
    .once('value');
  let user3 = admin
    .database()
    .ref('users/user3/userDetails')
    .once('value');
  let user4 = admin
    .database()
    .ref('users/user4/userDetails')
    .once('value');

  Promise.all([user1, user2, user3, user4])
    .then(result => {
      let data = {};
      result[0].forEach(action => {
        data['users/user1/userDetails/' + action.key + '/' + 'Rating'] = 0;
      });
      result[1].forEach(action => {
        data['users/user2/userDetails/' + action.key + '/' + 'Rating'] = 0;
      });
      result[2].forEach(action => {
        data['users/user3/userDetails/' + action.key + '/' + 'Rating'] = 0;
      });
      result[3].forEach(action => {
        data['users/user4/userDetails/' + action.key + '/' + 'Rating'] = 0;
      });
      console.log(data);
      return admin
        .database()
        .ref()
        .update(data);
    })
    .then(() => {
      return res.send('done');
    })
    .catch(error => {
      return res.status(500).send(error);
    });
});

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