How to get all the books of a specific author from my database?
Here is a snapshot of my database, i want to get "Colson Whitehead"
for web development, javascript.
To get all books by author Colson Whitehead, you do a query like this:
var query = firebase.database().ref("books").orderByChild("author").equalTo("Colson Whitehead");
query.on("value", function(snapshot) {
snapshot.forEach(function(bookSnapshot) {
console.log(bookSnapshot.key+": "+bookSnapshot.val());
});
})
This callback will get called initially and then every time something about the books by Colson Whitehead changes. If you only want the initial call, use once instead of on.
Related
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.
I know this is a very basic question, but I am new to reactjs and firebase. After two days I am giving up on searching for an answer myself.
I have a firestore database 'MusicList' containing several documents. Within these documents the field 'seperatedStrings' holds (you'll probably will be guessing it) artists and titles of songs.
image of the firestore database
How do I retrive the value a single field (in this example 'seperatedString') using the ID (in this example '5ajnHZ8YDaiYvTAvJ1ec'?
Whenever I am querying for this:
firestore().doc("MusicList/" + "5ajnHZ8YDaiYvTAvJ1ec").seperatedString
the console claims that it is "undefined" (That is a lie!), I'd expect 'Phil Upchurch Blackgold'
Thanks in advance.
You are not using Firebase queries correctly. If you want to fetch a document from a collection, you would do so like this:
firebase
.firestore()
.collection('MusicList')
.doc('5ajnHZ8YDaiYvTAvJ1ec')
.get()
.then(doc => {
if (doc && doc.exists) {
const { separatedString } = doc.data();
//use separatedString
}
});
Check out Firestore - Get A Document for more information.
Also, it would probably be better to structure your documents differently. You could have an artist and track field, and use doc.data().artist and doc.data().track respectively.
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.
I have some data in my firebase database that I want to retrieve. I did one big push so that all instances would have a unique id. I know want to retrieve this data
I know I can do this:
var gymData = firebase.database().ref('gymData/previousWorkouts')
but when I get returned data like this:
how the hell am I suppose to access that id without knowing it?
for example a user of my app is going to search for an exercise and update it. they search by name, how do I find that id and look inside there? I don't get it :/
firebase.database().ref('gymData/exrcises').orderByChild('name').equal("barbell bench press").once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;//this is id
});
});
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.