how to check if value exists in realtime database firebase - javascript

I have created a realtime database on firebase and having no issues adding and removing data in tables etc.
I currently have it setup like this:
So my goal is to check if a given value is inside my database currently.
for example, I would like to check if 'max' is currently a username in my database.
var data = db.ref('loginInfo/');
data.on('value', function(snapshot) {
_this.users = snapshot.val()
});
That is how I get all the values, it is saved into _this.users
(How do i check if a value is inside this object, i am making a login program)
if i console.log the object, this is what I see:
image

If you want to check if a child node exists under loginInfo where the username property has a value of max, you can use the following query for that:
var ref = db.ref('loginInfo/');
var query = ref.orderByChild('username').equalTo('max');
query.once('value', function(snapshot) {
console.log(snapshot.exists());
});
I'd also recommend reading the Firebase documentation on queries, as there are many more options.

Related

How can I get the id of a inserted document firebase-admin

I am handling some documents with firebase realtime database. I need to delete a document that I don't have the access to read it client side via SDK. In order to do that I need to know what is the id, so I should store it in my db (mongo) and when I need to delete a document on firebase I just get it from my DB and the I delete it.
I took a look to this answer but it doesn't work for me.
I insert documents into my firebase DB with this method (server side using firebase-admin)
const writeNotification = (uid: string, notification: INotification) => {
const db = admin.database();
const notsRef = db.ref(`notifications/${uid}`);
notsRef.push({
..._.omit(notification, 'to'),
});
};
If I do notsRef.id I get undefined.
How can I get the ID of my document that I have just inserted?
p.s. my documents are organized like this:
The answer you are looking at is about Firestore and not Realtime Database that you are using. The Reference has a key property and not id:
console.log('New Key', notsRef.key)
// to get child node's key
const childNotesRef = notsRef.push({
..._.omit(notification, 'to'),
});
console.log(childNotesRef.key)

Add field to document using cloud functions

I wanted to make the id of each document as a field of that document so that I can store it inside the doc. here is the cloud function I created:
exports.assignPID = functions.database
.ref('/players/{playerId}')
.onCreate((snapshot,context)=>{
const playerId = context.params.playerId;
console.log("new player "+playerId);
// const data = snapshot.val();
return snapshot.ref.update({'pid': playerId})
})
this deploys without any errors but whenever I add a new document to the 'players' collection there is no change in the document whatsoever
In your question, you use the word "document" to describe your data, which is a Firestore concept, and you've tagged this question google-cloud-firestore. However, the code you've written is for Realtime Database, which is a completely different product. So it will never run in response to changes in Firestore.
When you declare a function with functions.database, that's means Realtime Database. Instead, you should declare a Firestore trigger with functions.firestore. Please read the linked documentation for information about how to do that.

how do i grab push ID from database firebase

how do i grab the PUSH ID generated from firebase push method. I need to store this key in my App.js file for queries in the future. im not sure how to grab this key when the user is already logged in. I posted a pic of what im reffering to, to clarify
To get the pushid based on the email provided, then try the following:
firebase.database().ref().child("users").orderByChild("email").equalTo(yourEmail).on("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var randomKey=childSnapshot.key;
});
});
The snapshot is at the node users then you loop inside the keys and retrieve them using the key property
https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#key

Querying firebase database to retrieve user name

I am working on firebase for the first time. I am confused in querying the database. I have two objects on the database, one is the Auth and second one is the Chats. In Auths I have number of UID(user id) nodes, each of these nodes have their respective username. What I am trying to do is, I want to query that Auth object and get all the usernames which is equal to the one I take from a user through input box. In my sql it will be simple as SELECT * FROM auth WHERE username = userInputValue. I need same query like this, Below is what I have done so far.
var _firbaseDB = firebase.database(),
_firebaseAuthRef = _firbaseDB.ref("Auth/");
$("body").on("submit",".search-from",function(e){
e.preventDefault();
var ev = e.target,
_allUsers = $(ev).serializeArray()
_user = _allUsers[0].value;
_firebaseAuthRef.equalTo(_user).on("child_added",function(res){
console.log(res.val());
})
})
You were almost there. What's missing is that you need to specify which attribute you're querying on:
_firebaseAuthRef.orderByChild("n").equalTo(_user).on("child_‌​added",function(res)‌​{
console.log(res.val());
})
More info on the Firebase Database Documentation.

Get initial collection values from Firebase

In my node app I try to get access to Firebase, which contains a few collections.
var firebase = require('firebase');
firebase.initializeApp({myConfig: "Here"});
var database = firebase.database();
var rootRef = firebase.database().ref()
How exactly do i get all rows of a particular collection or all collections in database? Printing those variables gives strange structured objects.
You should totally be looking into firebase documentation to get this information.
The way you retrieve will depend on what exact behavior you are expecting. And the documentation is excential to understand how firebase behave as a database in wich one of the possible cases.
var rootRef = firebase.database().ref().on('value', function(snapshot) {
console.log(snapshot.val());
});
Snippet above will look into any change on your entire database (since you are not specifying any child like ref().child("users")) and log it as a javascript Object.
Good luck and, again, go to the documentation. :)

Categories

Resources