firebase admin with node.js: update in nested JSON tree - javascript

I use the below code on my Node.js admin server to retrieve data from my JSON tree, which works fine and prints the content of all snapshot2's to the console.
ref.once("value", function(snapshot0) {
snapshot0.forEach( function(snapshot1) {
snapshot1.child("Food").forEach( function(snapshot2) {
console.log(snapshot2.val());
});
});
});
However, how can I edit the data held at snapshot2?
If I try to call e.g.
snapshot2.update({250:42})
then it gives me the following error:
TypeError: snapshot2.update is not a function
I am really confused and think this must be a problem many people have?

You cannot update a snapshot. but you can update a reference, and you can get reference from snapshot by doing
snapshot.ref
, then you can do this to update
snapshot.ref.update({250:42})

Related

Ag-grid onPasteStart event access to paste data for async process

Am trying to access user paste data when onPasteStart/onPasteEnd event fires. Running Angular 12. The resulting javascript object includes all ag-grid methods and I cant find the resulting data that is pasted. I cant print the string of the object and ctrl-f because it gives an error with the object being circular.
https://www.ag-grid.com/angular-data-grid/clipboard/#clipboard-events
My intention is to take the paste and use resulting data to make a api request and then use the response to modify the paste. Tried using ag-grid processDataFromClipboard but came across errors as I imagine it doesnt support async functions.
Here is an angular plnkr example. All I need is to consol.log params.value with the pasted data and starting row of the paste.
https://plnkr.co/edit/q6cZQbX7LdHzozy9
console.log('Callback onPasteStart:', params);
}
onPasteEnd(params) {
console.log('Callback onPasteEnd:', params);
}
It sounds simple so I feel like I must be missing an obvious solution

Trying to get a snapshot of a variable from firebase gives me an error

Problem
In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:
firebase.database().ref('posts').child(this.state.passKey).update({
comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
When I actually run this code, I get an error saying:
Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.
My Server
-
To get the comments of particular post you should do like this
let postId='someId'
postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
});
It looks like you're expecting this bit of code to query the database:
firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
Unfortunately, it doesn't work that way. There's no snapshot property on a database Reference object returned by child() or ref().
Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.
var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
// use the snapshot here
})

Mongo DB - Why my Users.findOne is Undefined?

I'm working on Meteor, trying to find some values from Mongodb collection.
here is the code:
var sameLogins = Users.findOne({login: 'a'});
console.log(sameLogins);
But it's returning and "undefined".
But record exists in collection:
So, can anybody tell what I'm missing?
Also, in mongo console - everything is working fine:
I was looking in Publish/Subsribe stuff, but i'm using autopublish module yet.
Thank you!
I will leave the answer for this issue for new users having the same problem.
If you're using autopublish package then you should be aware that it's publishing the result of .find() for every collection.
But, Meteor.users.find(), be default, will return only _id and profile fields, so documents in your Meteor.users client collection will have these two fields only.
The most easy workaround for this would be to create your own publication (allUsers, for example) and in it to return those fields you need:
Server:
Meteor.publish('allUsers', () => {
// check for Meteor.userId() is omitted, put it here, if needed
return Meteor.users.find({}, { fields: { ... } });
});
Don't forget to subscribe to it:
Client:
Meteor.subscribe('allUsers');
Update for Meteor:
Right now you are storing a cursor in your variable sameLogins. In order to retrieve the results you want, you must actually execute this query by either calling fetch(). What is returned from findOne without fetch is essentially an object that you could use to iterate over and find mongoDB documents - (called a collection cursor). The cursor is not your result itself.
Calling fetch would like something like:
Users.findOne({login: 'a'}).fetch()

Cloud code not working

i am new at parse..trying code..trying to run a trigger required in my project.but not able to track not even i am getting any error.
i am using cloud code i.e triggers...
what i want to do is, after update or save i want to run a trigger which will a column in a class with value of 200.
Parse.initialize('APPLICATION_ID', 'JAVASCRIPT_KEY');
Parse.Cloud.afterSave("match_status", function(request)
{
var query = new Parse.Query('Wallet');
query.set("wallet_coines_number", 200);
query.equalTo("objectId", "FrbLo6v5ux");
query.save();
});
i am using afterSave trigger in which match_status is my trigger name. after that i making a object called query of Wallet class. This object will set column 'wallet_coines_number' with the value 200 where objectId is FrbLo6v5ux. after that i used save function which will execute query.
Please guide me if i am wrong, or following wrong approach.
Thank You !
Have you read the Parse documentation on Cloud Code ?
The first line of your code is only relevant when you are initialising Parse JavaScript SDK in a web page, you do not need to initialise anything in Parse cloud code in the main.js file. Also you cannot use a query to save/update an object. A query is for searching/finding objects, when you want to save or update an object you need to create a Parse.Object and save that.
So you code should become something like:
Parse.Cloud.afterSave("match_status", function(request) {
var wallet = new Parse.Object('Wallet');
wallet.set("wallet_coines_number", 200);
wallet.set("objectId", "FrbLo6v5ux");
wallet.save();
});

Unable to update an entry in the firebase list

After few attempts, I am still unable to find the right way to update a list in firebase.
I have my dev account in firebase. But,just for this question I used the sample on the public firebase url:
var url1= new Firebase("https://samplechat.firebaseio-demo.com/message_list");
url1.push({text:'Its working',user_id:'123'});
setTimeout(function (){
url1.on('child_added', function(snap) {
snap.update({text:'Its not working',user_id:'123'});
snap.child('text').update('Its not working');
console.log('messages in range', snap.val());}
)},6000);
I am able to set the data, but when the child_added function is triggered for the messages. I am getting the below javascript error:
Uncaught TypeError: undefined is not a function
What can be the issue ?
You aren't using the data snapshot object correctly within your function.
https://www.firebase.com/docs/javascript/datasnapshot/index.html
Change it to
snap.ref().update({text: '....', user_id: '...'});
If you were just looking to get the value, you'd use
snap.val()
please provide more information on what you're trying to accomplish. Right now you're trying to update your snapshot(snap.update). I'm not sure what that's for. Instead of calling your snap, you can call your reference, url1.update. Also, take another look at the docs for reading data, https://www.firebase.com/docs/reading-data.html and writing data, https://www.firebase.com/docs/writing-data.html. Looks like you've combined functions from reading and writing data.

Categories

Resources