submariner
submariner

Reputation: 400

Putting variable in multiple path

I am new to javascript and have a code right here which works really great. But it puts the variable k only to one path. I also want to add it to few more paths.

This here with one path works great.

function checkpath() {
   var db = admin.database();
    var ref =  db.ref('placeID/here:pds:place:276u0wuu-237aa2a/' + messageID + '/notification');
    ref.get()
    .then((snapshot) => {
      if (snapshot.exists()) {
        const keyArray = Object.keys(snapshot.val());
        return Promise.all(keyArray.map(k => db.ref('/user/' + k + '/notification/' + subMessageID + '/subMessageID/').set(subMessageID)));
      } else {
        console.log('No data available');
        return null;
        
      }
    })
}

This one does not work. It gives me the error: "Exception from a finished function: ReferenceError: k is not defined"

function checkpath() {
    var db = admin.database();
    var ref =  db.ref('placeID/here:pds:place:276u0wuu-237aa2a/' + messageID + '/notification');
    ref.get()
    .then((snapshot) => {
      if (snapshot.exists()) {
        const keyArray = Object.keys(snapshot.val());
        return Promise.all(keyArray.map(k => 
            
            (db.ref('/user/' + k + '/notification/' + subMessageID + '/subMessageID/').set(subMessageID)),
            (db.ref('/user/' + k + '/notification/' + subMessageID + '/userTime/').set(userTime))
        
        ));
      } else {
        console.log('No data available');
        return null;
        
      }
    })
}

Upvotes: 0

Views: 227

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83173

This is because your callback function returns two Objects (here two Promises) separated by a comma.

Let's illustrate the problem with the following code snippets:

const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => (x * 2, x * 3)); // See the parenthesis
console.log(map1);

returns > Array [3, 12, 27, 48]

because the anonymous callback function returns the second expression i.e. x * 3.

Now if you remove the parenthesis as follows

const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2, x * 3); 
console.log(map1);

it returns Error: x is not defined because with just x *3, x is not defined (while it is defined within x => x * 2)


However, in your case, you cannot add parenthesis because you want to get the two Promises added to the Array you're going to pass to Promise.all().

Therefore you need to do something along the following lines, by pushing the promises in an Array:

  function checkpath() {
    var db = admin.database();
    var ref = db.ref(
      'placeID/here:pds:place:276u0wuu-237aa2a/' +
        messageID +
        '/notification'
    );
    return ref.get().then((snapshot) => {
      if (snapshot.exists()) {
        const keyArray = Object.keys(snapshot.val());
        const promises = [];
        keyArray.forEach((k) => {
          promises.push(
            db
              .ref(
                '/user/' +
                  k +
                  '/notification/' +
                  subMessageID +
                  '/subMessageID/'
              )
              .set(subMessageID)
          );
          promises.push(
            db
              .ref(
                '/user/' +
                  k +
                  '/notification/' +
                  subMessageID +
                  '/userTime/'
              )
              .set(userTime)
          );
        });
        return Promise.all(promises);  // Not sure you need to return anything here because the set() method returns a Promise<void>...
      } else {
        console.log('No data available');
        return null;
      }
    });
  }

Upvotes: 1

Related Questions