Firebase Update resultset NodeJS
Why is it that I can't seem to update a result set. I have 1000 items and I query Firebase and get back a DataSnapShot array. I want to check each element of the array and if a condition exists, then change the "type" to a "C". I get no errors, yet the update does not appear to have been applied. I'm doing this in the promise that is returned and yet the update is "ignored".
var db = admin.database();
var ref = db.ref("/calendars");
ref.once("value", function(snapshot) {
snapshot.forEach(function(data) {
var obj = data.val();
if (someconditionIsTrue) {
var objToUpdate = `calendars\${data.key}`;
console.log(`Should update ${objToUpdate}`);
var entryRef = db.ref("/calendars").child(data.key).child('type');
entryRef.transaction(function(t) {
console.log(`TYPE: ${t}`); // DISPLAYS NULL EVEN THOUGH THERE IS A CHAR IN DB
return "C"; // Should change TYPE in the record to a "C"
});
}
Also tried just to do a set inside the forEach at well but that does not work either (which is why I thought i had to use a transaction).
node.js firebase-realtime-database nosql firebase-admin
add a comment |
Why is it that I can't seem to update a result set. I have 1000 items and I query Firebase and get back a DataSnapShot array. I want to check each element of the array and if a condition exists, then change the "type" to a "C". I get no errors, yet the update does not appear to have been applied. I'm doing this in the promise that is returned and yet the update is "ignored".
var db = admin.database();
var ref = db.ref("/calendars");
ref.once("value", function(snapshot) {
snapshot.forEach(function(data) {
var obj = data.val();
if (someconditionIsTrue) {
var objToUpdate = `calendars\${data.key}`;
console.log(`Should update ${objToUpdate}`);
var entryRef = db.ref("/calendars").child(data.key).child('type');
entryRef.transaction(function(t) {
console.log(`TYPE: ${t}`); // DISPLAYS NULL EVEN THOUGH THERE IS A CHAR IN DB
return "C"; // Should change TYPE in the record to a "C"
});
}
Also tried just to do a set inside the forEach at well but that does not work either (which is why I thought i had to use a transaction).
node.js firebase-realtime-database nosql firebase-admin
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will benull
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…
– Frank van Puffelen
Nov 21 '18 at 16:21
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
I don't see any promise in the code you shared. ButReference.transaction
returns a promise, so it should work nicely with other promises (e.g. in aPromise.all()
call).
– Frank van Puffelen
Nov 23 '18 at 15:16
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33
add a comment |
Why is it that I can't seem to update a result set. I have 1000 items and I query Firebase and get back a DataSnapShot array. I want to check each element of the array and if a condition exists, then change the "type" to a "C". I get no errors, yet the update does not appear to have been applied. I'm doing this in the promise that is returned and yet the update is "ignored".
var db = admin.database();
var ref = db.ref("/calendars");
ref.once("value", function(snapshot) {
snapshot.forEach(function(data) {
var obj = data.val();
if (someconditionIsTrue) {
var objToUpdate = `calendars\${data.key}`;
console.log(`Should update ${objToUpdate}`);
var entryRef = db.ref("/calendars").child(data.key).child('type');
entryRef.transaction(function(t) {
console.log(`TYPE: ${t}`); // DISPLAYS NULL EVEN THOUGH THERE IS A CHAR IN DB
return "C"; // Should change TYPE in the record to a "C"
});
}
Also tried just to do a set inside the forEach at well but that does not work either (which is why I thought i had to use a transaction).
node.js firebase-realtime-database nosql firebase-admin
Why is it that I can't seem to update a result set. I have 1000 items and I query Firebase and get back a DataSnapShot array. I want to check each element of the array and if a condition exists, then change the "type" to a "C". I get no errors, yet the update does not appear to have been applied. I'm doing this in the promise that is returned and yet the update is "ignored".
var db = admin.database();
var ref = db.ref("/calendars");
ref.once("value", function(snapshot) {
snapshot.forEach(function(data) {
var obj = data.val();
if (someconditionIsTrue) {
var objToUpdate = `calendars\${data.key}`;
console.log(`Should update ${objToUpdate}`);
var entryRef = db.ref("/calendars").child(data.key).child('type');
entryRef.transaction(function(t) {
console.log(`TYPE: ${t}`); // DISPLAYS NULL EVEN THOUGH THERE IS A CHAR IN DB
return "C"; // Should change TYPE in the record to a "C"
});
}
Also tried just to do a set inside the forEach at well but that does not work either (which is why I thought i had to use a transaction).
node.js firebase-realtime-database nosql firebase-admin
node.js firebase-realtime-database nosql firebase-admin
edited Nov 21 '18 at 16:25
Frank van Puffelen
227k28372396
227k28372396
asked Nov 21 '18 at 15:31
justdan0227
6161028
6161028
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will benull
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…
– Frank van Puffelen
Nov 21 '18 at 16:21
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
I don't see any promise in the code you shared. ButReference.transaction
returns a promise, so it should work nicely with other promises (e.g. in aPromise.all()
call).
– Frank van Puffelen
Nov 23 '18 at 15:16
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33
add a comment |
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will benull
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…
– Frank van Puffelen
Nov 21 '18 at 16:21
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
I don't see any promise in the code you shared. ButReference.transaction
returns a promise, so it should work nicely with other promises (e.g. in aPromise.all()
call).
– Frank van Puffelen
Nov 23 '18 at 15:16
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will be
null
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…– Frank van Puffelen
Nov 21 '18 at 16:21
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will be
null
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…– Frank van Puffelen
Nov 21 '18 at 16:21
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
I don't see any promise in the code you shared. But
Reference.transaction
returns a promise, so it should work nicely with other promises (e.g. in a Promise.all()
call).– Frank van Puffelen
Nov 23 '18 at 15:16
I don't see any promise in the code you shared. But
Reference.transaction
returns a promise, so it should work nicely with other promises (e.g. in a Promise.all()
call).– Frank van Puffelen
Nov 23 '18 at 15:16
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415425%2ffirebase-update-resultset-nodejs%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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415425%2ffirebase-update-resultset-nodejs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will be
null
. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/…– Frank van Puffelen
Nov 21 '18 at 16:21
What is the condition you are trying to check?
– Frank van Puffelen
Nov 21 '18 at 16:25
The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise?
– justdan0227
Nov 23 '18 at 14:30
I don't see any promise in the code you shared. But
Reference.transaction
returns a promise, so it should work nicely with other promises (e.g. in aPromise.all()
call).– Frank van Puffelen
Nov 23 '18 at 15:16
Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB
– justdan0227
Nov 25 '18 at 15:33