Cannot read property ref of undefined and Firebase upgrade on Win 10










1















I have two Firebase Cloud functions, one is written in "old" version, and one -- in "new":



exports.recountTotalCalories = functions.database.ref('/mealsOf/userId/day/meals/mealId')
.onWrite(
async( change ) =>
const collectionRef= change.after.ref.parent;
const counterRef= collectionRef.ref.parent.ref.child('totalCalories');
let finalSum = await (collectionRef.ref.once('value').then( (snap) =>
let sum= 0;
snap.forEach( (child) =>
console.log(child.val().numCalories);
sum= sum+parseInt(child.val().numCalories);
)
return sum ;
));
return await counterRef.set(finalSum);

);

exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
.onWrite(
async(change,context) =>
const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
let calorieLimit = await rootRef.once('value').then( (snapshot) =>
return snapshot.val();
);
let totalCalories = change.after.val();
const collectionRef = change.after.ref.parent.ref.child('meals');
return await collectionRef.ref.once('value').then ( (snapshot) =>
snapshot.forEach( (child) =>
console.log(totalCalories);
console.log(calorieLimit);
child.redZone = totalCalories > calorieLimit;
console.log(child.redZone);
);
);

);


The first function works fine, it is the second that gives me trouble by saying "cannot read the property ref of undefined", which I found out by exploring the logs in Firebase console:



TypeError: Cannot read property 'ref' of undefined
at exports.updateColors.functions.database.ref.onWrite (/srv/index.js:105:47)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at /worker/worker.js:756:24
at <anonymous>
at process._tickDomainCallback (internal/


So, a certain SO thread firebase cloud functions Cannot read property 'ref' of undefined recommends "upgrading" Firebase cloud functions. But I am not sure just how. I have tried issuing the commands suggested here: Firebase functions: cannot read property 'user_id' of undefined on my machine (Windows 10), it seemed to have done some work, but the error persists on the Realtime Database. What am I missing?



EDIT: the log entry refers to line const collectionRef = change.after.ref.parent.child('meals');, and even more precisely -- to change.after.ref. But We are already accessing this in the beginning of the function, why does not it complain about it then?



EDIT': Here is my package.json file:




"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts":
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
,
"dependencies":
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
,
"private": true,
"engines":
"node": "8"




EDIT'': I re-wrote my second function using async (see the update above) and now it seems to execute all-right, except that the log is full of false promises and the desired change is not made to the database, anyway:
enter image description here



EDIT''': Now, this is the latest version of my second function, which finally does the trick:



exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
.onWrite(
async(change,context) =>
const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
let calorieLimit = await rootRef.once('value').then( (snapshot) =>
return snapshot.val();
);
let totalCalories = await change.after.ref.once('value').then((snap) =>
return snap.val();
);
const collectionRef = change.after.ref.parent.ref.child('meals');
return await collectionRef.ref.once('value').then ( (snapshot) =>
snapshot.forEach( (child) =>
console.log(totalCalories);
console.log(calorieLimit);
child.ref.child('redZone').ref.set(totalCalories > calorieLimit);
console.log(child.val());
);
);

);


Getting the hang of JavaScript is tricky, it seems.










share|improve this question




























    1















    I have two Firebase Cloud functions, one is written in "old" version, and one -- in "new":



    exports.recountTotalCalories = functions.database.ref('/mealsOf/userId/day/meals/mealId')
    .onWrite(
    async( change ) =>
    const collectionRef= change.after.ref.parent;
    const counterRef= collectionRef.ref.parent.ref.child('totalCalories');
    let finalSum = await (collectionRef.ref.once('value').then( (snap) =>
    let sum= 0;
    snap.forEach( (child) =>
    console.log(child.val().numCalories);
    sum= sum+parseInt(child.val().numCalories);
    )
    return sum ;
    ));
    return await counterRef.set(finalSum);

    );

    exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
    .onWrite(
    async(change,context) =>
    const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
    let calorieLimit = await rootRef.once('value').then( (snapshot) =>
    return snapshot.val();
    );
    let totalCalories = change.after.val();
    const collectionRef = change.after.ref.parent.ref.child('meals');
    return await collectionRef.ref.once('value').then ( (snapshot) =>
    snapshot.forEach( (child) =>
    console.log(totalCalories);
    console.log(calorieLimit);
    child.redZone = totalCalories > calorieLimit;
    console.log(child.redZone);
    );
    );

    );


    The first function works fine, it is the second that gives me trouble by saying "cannot read the property ref of undefined", which I found out by exploring the logs in Firebase console:



    TypeError: Cannot read property 'ref' of undefined
    at exports.updateColors.functions.database.ref.onWrite (/srv/index.js:105:47)
    at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at /worker/worker.js:756:24
    at <anonymous>
    at process._tickDomainCallback (internal/


    So, a certain SO thread firebase cloud functions Cannot read property 'ref' of undefined recommends "upgrading" Firebase cloud functions. But I am not sure just how. I have tried issuing the commands suggested here: Firebase functions: cannot read property 'user_id' of undefined on my machine (Windows 10), it seemed to have done some work, but the error persists on the Realtime Database. What am I missing?



    EDIT: the log entry refers to line const collectionRef = change.after.ref.parent.child('meals');, and even more precisely -- to change.after.ref. But We are already accessing this in the beginning of the function, why does not it complain about it then?



    EDIT': Here is my package.json file:




    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts":
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
    ,
    "dependencies":
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
    ,
    "private": true,
    "engines":
    "node": "8"




    EDIT'': I re-wrote my second function using async (see the update above) and now it seems to execute all-right, except that the log is full of false promises and the desired change is not made to the database, anyway:
    enter image description here



    EDIT''': Now, this is the latest version of my second function, which finally does the trick:



    exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
    .onWrite(
    async(change,context) =>
    const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
    let calorieLimit = await rootRef.once('value').then( (snapshot) =>
    return snapshot.val();
    );
    let totalCalories = await change.after.ref.once('value').then((snap) =>
    return snap.val();
    );
    const collectionRef = change.after.ref.parent.ref.child('meals');
    return await collectionRef.ref.once('value').then ( (snapshot) =>
    snapshot.forEach( (child) =>
    console.log(totalCalories);
    console.log(calorieLimit);
    child.ref.child('redZone').ref.set(totalCalories > calorieLimit);
    console.log(child.val());
    );
    );

    );


    Getting the hang of JavaScript is tricky, it seems.










    share|improve this question


























      1












      1








      1








      I have two Firebase Cloud functions, one is written in "old" version, and one -- in "new":



      exports.recountTotalCalories = functions.database.ref('/mealsOf/userId/day/meals/mealId')
      .onWrite(
      async( change ) =>
      const collectionRef= change.after.ref.parent;
      const counterRef= collectionRef.ref.parent.ref.child('totalCalories');
      let finalSum = await (collectionRef.ref.once('value').then( (snap) =>
      let sum= 0;
      snap.forEach( (child) =>
      console.log(child.val().numCalories);
      sum= sum+parseInt(child.val().numCalories);
      )
      return sum ;
      ));
      return await counterRef.set(finalSum);

      );

      exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
      .onWrite(
      async(change,context) =>
      const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
      let calorieLimit = await rootRef.once('value').then( (snapshot) =>
      return snapshot.val();
      );
      let totalCalories = change.after.val();
      const collectionRef = change.after.ref.parent.ref.child('meals');
      return await collectionRef.ref.once('value').then ( (snapshot) =>
      snapshot.forEach( (child) =>
      console.log(totalCalories);
      console.log(calorieLimit);
      child.redZone = totalCalories > calorieLimit;
      console.log(child.redZone);
      );
      );

      );


      The first function works fine, it is the second that gives me trouble by saying "cannot read the property ref of undefined", which I found out by exploring the logs in Firebase console:



      TypeError: Cannot read property 'ref' of undefined
      at exports.updateColors.functions.database.ref.onWrite (/srv/index.js:105:47)
      at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
      at /worker/worker.js:756:24
      at <anonymous>
      at process._tickDomainCallback (internal/


      So, a certain SO thread firebase cloud functions Cannot read property 'ref' of undefined recommends "upgrading" Firebase cloud functions. But I am not sure just how. I have tried issuing the commands suggested here: Firebase functions: cannot read property 'user_id' of undefined on my machine (Windows 10), it seemed to have done some work, but the error persists on the Realtime Database. What am I missing?



      EDIT: the log entry refers to line const collectionRef = change.after.ref.parent.child('meals');, and even more precisely -- to change.after.ref. But We are already accessing this in the beginning of the function, why does not it complain about it then?



      EDIT': Here is my package.json file:




      "name": "functions",
      "description": "Cloud Functions for Firebase",
      "scripts":
      "serve": "firebase serve --only functions",
      "shell": "firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
      ,
      "dependencies":
      "firebase-admin": "~6.0.0",
      "firebase-functions": "^2.1.0"
      ,
      "private": true,
      "engines":
      "node": "8"




      EDIT'': I re-wrote my second function using async (see the update above) and now it seems to execute all-right, except that the log is full of false promises and the desired change is not made to the database, anyway:
      enter image description here



      EDIT''': Now, this is the latest version of my second function, which finally does the trick:



      exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
      .onWrite(
      async(change,context) =>
      const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
      let calorieLimit = await rootRef.once('value').then( (snapshot) =>
      return snapshot.val();
      );
      let totalCalories = await change.after.ref.once('value').then((snap) =>
      return snap.val();
      );
      const collectionRef = change.after.ref.parent.ref.child('meals');
      return await collectionRef.ref.once('value').then ( (snapshot) =>
      snapshot.forEach( (child) =>
      console.log(totalCalories);
      console.log(calorieLimit);
      child.ref.child('redZone').ref.set(totalCalories > calorieLimit);
      console.log(child.val());
      );
      );

      );


      Getting the hang of JavaScript is tricky, it seems.










      share|improve this question
















      I have two Firebase Cloud functions, one is written in "old" version, and one -- in "new":



      exports.recountTotalCalories = functions.database.ref('/mealsOf/userId/day/meals/mealId')
      .onWrite(
      async( change ) =>
      const collectionRef= change.after.ref.parent;
      const counterRef= collectionRef.ref.parent.ref.child('totalCalories');
      let finalSum = await (collectionRef.ref.once('value').then( (snap) =>
      let sum= 0;
      snap.forEach( (child) =>
      console.log(child.val().numCalories);
      sum= sum+parseInt(child.val().numCalories);
      )
      return sum ;
      ));
      return await counterRef.set(finalSum);

      );

      exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
      .onWrite(
      async(change,context) =>
      const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
      let calorieLimit = await rootRef.once('value').then( (snapshot) =>
      return snapshot.val();
      );
      let totalCalories = change.after.val();
      const collectionRef = change.after.ref.parent.ref.child('meals');
      return await collectionRef.ref.once('value').then ( (snapshot) =>
      snapshot.forEach( (child) =>
      console.log(totalCalories);
      console.log(calorieLimit);
      child.redZone = totalCalories > calorieLimit;
      console.log(child.redZone);
      );
      );

      );


      The first function works fine, it is the second that gives me trouble by saying "cannot read the property ref of undefined", which I found out by exploring the logs in Firebase console:



      TypeError: Cannot read property 'ref' of undefined
      at exports.updateColors.functions.database.ref.onWrite (/srv/index.js:105:47)
      at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
      at /worker/worker.js:756:24
      at <anonymous>
      at process._tickDomainCallback (internal/


      So, a certain SO thread firebase cloud functions Cannot read property 'ref' of undefined recommends "upgrading" Firebase cloud functions. But I am not sure just how. I have tried issuing the commands suggested here: Firebase functions: cannot read property 'user_id' of undefined on my machine (Windows 10), it seemed to have done some work, but the error persists on the Realtime Database. What am I missing?



      EDIT: the log entry refers to line const collectionRef = change.after.ref.parent.child('meals');, and even more precisely -- to change.after.ref. But We are already accessing this in the beginning of the function, why does not it complain about it then?



      EDIT': Here is my package.json file:




      "name": "functions",
      "description": "Cloud Functions for Firebase",
      "scripts":
      "serve": "firebase serve --only functions",
      "shell": "firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
      ,
      "dependencies":
      "firebase-admin": "~6.0.0",
      "firebase-functions": "^2.1.0"
      ,
      "private": true,
      "engines":
      "node": "8"




      EDIT'': I re-wrote my second function using async (see the update above) and now it seems to execute all-right, except that the log is full of false promises and the desired change is not made to the database, anyway:
      enter image description here



      EDIT''': Now, this is the latest version of my second function, which finally does the trick:



      exports.updateColors = functions.database.ref('/mealsOf/userId/day/totalCalories')
      .onWrite(
      async(change,context) =>
      const rootRef = change.after.ref.root.child('userInfo/'+context.params.userId);
      let calorieLimit = await rootRef.once('value').then( (snapshot) =>
      return snapshot.val();
      );
      let totalCalories = await change.after.ref.once('value').then((snap) =>
      return snap.val();
      );
      const collectionRef = change.after.ref.parent.ref.child('meals');
      return await collectionRef.ref.once('value').then ( (snapshot) =>
      snapshot.forEach( (child) =>
      console.log(totalCalories);
      console.log(calorieLimit);
      child.ref.child('redZone').ref.set(totalCalories > calorieLimit);
      console.log(child.val());
      );
      );

      );


      Getting the hang of JavaScript is tricky, it seems.







      javascript firebase firebase-realtime-database google-cloud-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 14:42







      Ilonpilaaja

















      asked Nov 14 '18 at 13:36









      IlonpilaajaIlonpilaaja

      317214




      317214






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301518%2fcannot-read-property-ref-of-undefined-and-firebase-upgrade-on-win-10%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301518%2fcannot-read-property-ref-of-undefined-and-firebase-upgrade-on-win-10%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto