Firebase location triggers, Reference.set failed: First argument contains NaN in property












0














I have a bunch of location-listeners such as follows:



exports.recountTotalCaloriesOnUpdate = functions.database.ref('/mealsOf/{userId}/{day}/meals/{hour}/{mealId}')
.onUpdate(
async(change,context) => {
let uid= context.params.userId;
let day= context.params.day;
/*
let res= await dbroot.ref(`mealsOf/${uid}/${day}`).push('totalCalories').set(0).then( (e) => {
return null ;
});
*/
const collectionRef = change.after.ref.parent.ref.parent;
//const counterRef = dbroot.ref(`mealsOf/${uid}/${day}`).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.ref.transaction((cnt) => {
return finalSum;
});
*/
return await dbroot.ref(`mealsOf/${uid}/${day}/totalCalories`).set(finalSum).then( (a) => {
return null ;
});
}
);


My DB-schema looks like follows:



enter image description here



For example, when the first meal even for a particular day is created, I do understand that totalCalories field for it may not yet exists, and more often than not I get the error referring to return await dbroot.ref(mealsOf/${uid}/${day}/totalCalories).set(finalSum).then( (a) => {
return null ;
});
-line in the above code that says that everything before .set is NaN. I tried .push(<childneme>).set(0), too, but this error persists. I have many triggers that try to keep the DB in consistent state, e.g. cleaning up after functions.auth.user().onDelete() is called. So I think this .onWrite()-trigger might be interfering when a data being wiped out.
My question is, is the above code looks about correct for keeping the totalCalories field in consistent state?



EDIT: my guess is that the trigger is confused: when a user is deleted, my wipe-out function tries to delete the branch mealsOf/<userId> altogether, but this is considered as a write operation on all of its children, that is why the above trigger is launched and it sets the totalCalories to zero, but the reference is gone. That would explain why sometimes the mealsOf/<userId> branch would persist, whereas all the user-related data are gone after the deletion.










share|improve this question
























  • The question is resolved in stackoverflow.com/questions/53415790/…
    – Ilonpilaaja
    Nov 21 at 17:30
















0














I have a bunch of location-listeners such as follows:



exports.recountTotalCaloriesOnUpdate = functions.database.ref('/mealsOf/{userId}/{day}/meals/{hour}/{mealId}')
.onUpdate(
async(change,context) => {
let uid= context.params.userId;
let day= context.params.day;
/*
let res= await dbroot.ref(`mealsOf/${uid}/${day}`).push('totalCalories').set(0).then( (e) => {
return null ;
});
*/
const collectionRef = change.after.ref.parent.ref.parent;
//const counterRef = dbroot.ref(`mealsOf/${uid}/${day}`).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.ref.transaction((cnt) => {
return finalSum;
});
*/
return await dbroot.ref(`mealsOf/${uid}/${day}/totalCalories`).set(finalSum).then( (a) => {
return null ;
});
}
);


My DB-schema looks like follows:



enter image description here



For example, when the first meal even for a particular day is created, I do understand that totalCalories field for it may not yet exists, and more often than not I get the error referring to return await dbroot.ref(mealsOf/${uid}/${day}/totalCalories).set(finalSum).then( (a) => {
return null ;
});
-line in the above code that says that everything before .set is NaN. I tried .push(<childneme>).set(0), too, but this error persists. I have many triggers that try to keep the DB in consistent state, e.g. cleaning up after functions.auth.user().onDelete() is called. So I think this .onWrite()-trigger might be interfering when a data being wiped out.
My question is, is the above code looks about correct for keeping the totalCalories field in consistent state?



EDIT: my guess is that the trigger is confused: when a user is deleted, my wipe-out function tries to delete the branch mealsOf/<userId> altogether, but this is considered as a write operation on all of its children, that is why the above trigger is launched and it sets the totalCalories to zero, but the reference is gone. That would explain why sometimes the mealsOf/<userId> branch would persist, whereas all the user-related data are gone after the deletion.










share|improve this question
























  • The question is resolved in stackoverflow.com/questions/53415790/…
    – Ilonpilaaja
    Nov 21 at 17:30














0












0








0







I have a bunch of location-listeners such as follows:



exports.recountTotalCaloriesOnUpdate = functions.database.ref('/mealsOf/{userId}/{day}/meals/{hour}/{mealId}')
.onUpdate(
async(change,context) => {
let uid= context.params.userId;
let day= context.params.day;
/*
let res= await dbroot.ref(`mealsOf/${uid}/${day}`).push('totalCalories').set(0).then( (e) => {
return null ;
});
*/
const collectionRef = change.after.ref.parent.ref.parent;
//const counterRef = dbroot.ref(`mealsOf/${uid}/${day}`).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.ref.transaction((cnt) => {
return finalSum;
});
*/
return await dbroot.ref(`mealsOf/${uid}/${day}/totalCalories`).set(finalSum).then( (a) => {
return null ;
});
}
);


My DB-schema looks like follows:



enter image description here



For example, when the first meal even for a particular day is created, I do understand that totalCalories field for it may not yet exists, and more often than not I get the error referring to return await dbroot.ref(mealsOf/${uid}/${day}/totalCalories).set(finalSum).then( (a) => {
return null ;
});
-line in the above code that says that everything before .set is NaN. I tried .push(<childneme>).set(0), too, but this error persists. I have many triggers that try to keep the DB in consistent state, e.g. cleaning up after functions.auth.user().onDelete() is called. So I think this .onWrite()-trigger might be interfering when a data being wiped out.
My question is, is the above code looks about correct for keeping the totalCalories field in consistent state?



EDIT: my guess is that the trigger is confused: when a user is deleted, my wipe-out function tries to delete the branch mealsOf/<userId> altogether, but this is considered as a write operation on all of its children, that is why the above trigger is launched and it sets the totalCalories to zero, but the reference is gone. That would explain why sometimes the mealsOf/<userId> branch would persist, whereas all the user-related data are gone after the deletion.










share|improve this question















I have a bunch of location-listeners such as follows:



exports.recountTotalCaloriesOnUpdate = functions.database.ref('/mealsOf/{userId}/{day}/meals/{hour}/{mealId}')
.onUpdate(
async(change,context) => {
let uid= context.params.userId;
let day= context.params.day;
/*
let res= await dbroot.ref(`mealsOf/${uid}/${day}`).push('totalCalories').set(0).then( (e) => {
return null ;
});
*/
const collectionRef = change.after.ref.parent.ref.parent;
//const counterRef = dbroot.ref(`mealsOf/${uid}/${day}`).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.ref.transaction((cnt) => {
return finalSum;
});
*/
return await dbroot.ref(`mealsOf/${uid}/${day}/totalCalories`).set(finalSum).then( (a) => {
return null ;
});
}
);


My DB-schema looks like follows:



enter image description here



For example, when the first meal even for a particular day is created, I do understand that totalCalories field for it may not yet exists, and more often than not I get the error referring to return await dbroot.ref(mealsOf/${uid}/${day}/totalCalories).set(finalSum).then( (a) => {
return null ;
});
-line in the above code that says that everything before .set is NaN. I tried .push(<childneme>).set(0), too, but this error persists. I have many triggers that try to keep the DB in consistent state, e.g. cleaning up after functions.auth.user().onDelete() is called. So I think this .onWrite()-trigger might be interfering when a data being wiped out.
My question is, is the above code looks about correct for keeping the totalCalories field in consistent state?



EDIT: my guess is that the trigger is confused: when a user is deleted, my wipe-out function tries to delete the branch mealsOf/<userId> altogether, but this is considered as a write operation on all of its children, that is why the above trigger is launched and it sets the totalCalories to zero, but the reference is gone. That would explain why sometimes the mealsOf/<userId> branch would persist, whereas all the user-related data are gone after the deletion.







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 21 at 8:44









KENdi

5,7092821




5,7092821










asked Nov 21 at 5:10









Ilonpilaaja

322214




322214












  • The question is resolved in stackoverflow.com/questions/53415790/…
    – Ilonpilaaja
    Nov 21 at 17:30


















  • The question is resolved in stackoverflow.com/questions/53415790/…
    – Ilonpilaaja
    Nov 21 at 17:30
















The question is resolved in stackoverflow.com/questions/53415790/…
– Ilonpilaaja
Nov 21 at 17:30




The question is resolved in stackoverflow.com/questions/53415790/…
– Ilonpilaaja
Nov 21 at 17:30

















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%2f53405592%2ffirebase-location-triggers-reference-set-failed-first-argument-contains-nan-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53405592%2ffirebase-location-triggers-reference-set-failed-first-argument-contains-nan-in%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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python