During storing an object to my firebase, I am expecting the structure as image below, but what I get was a generated running number as a key. This is my code to store an object to firebase
var location = [];
location.push({
ms_jhr_1 : {
name: value
},
...
});
const a = firebase.database().ref('Food/'+id);
a.set(location);
How do I keep my structure without generate the running number?
The problem is you are using an array to store your data and then setting that array in firebase. To get the expected result you have to modify your code a little bit.
Here use this and remove other code
const a = firebase.database().ref('Food/'+id);
a.set(ms_jhr_1);
So you just need to pass the object you want to store under that id and not the whole array.
Note:- If you want to store multiple entries under one id then you have to push all those entries in an Object and not in array.
So it will look something like this
var location = {};
Now use for loop to insert all your data into this object (Remember, you are adding objects inside an object). You don't need array. Because in firebase data is stored in JSON tree format.
Hope it helps.
Related
So I have an array in a firebase document called ActivityLikes. It looks like this:
The collection is like this below:
const db = fire.firestore();
db.collection("eventLikes")
.doc(eventID)
.set({
ActivityLikes: likes
})
I have another collection which has the word "Avoca" saved, and I have this right now and I basically want to pull the data that has "Avoca" in it. So like with the name "Avoca" i can also get the imgURL. I've tried a for loop to go inside the object that firebase returns but I guess my logic is due to the fact theres multiple value and key pairs inside of it so how would i get all of the data inside of index 1 because it has the name = "Avoca".
I have the activitylikes in a hook so I have it after calling it from firebase.
I've tried like:
for (var i=ActivityLikes.length; i--;) {
if (myArr[i].indexOf("Avoca")>=0) break;
}
I think my logic is off Im just not sure how to get all of the data inside of the index once the name matches
The other collection just looks like this: https://i.stack.imgur.com/TqhWd.png. I know how to call this from firebase, I just need to figure out the logic for getting all the data in the array in the ActivityLikes when the name "Avoca" is present
If you are looking just for the word "Avoca" then try using find():
// data is snapshot.data() after query
const avocaData = data.ActivityLikes.find(d => d.name === "Avoca")
Here avocaData will either be the first object from likes array where name is Avoca or undefined is there isn't any. After that you can use an if statement to check that and proceed with rest of the logic.
I try to load the list of users in the following code
<div id="users" data-users='[{"name":"one","userName":"user_one"},
{"name":"two","userName":"user_two"},{"name":"three","userName":"user_three"}]'></div>
How can I load the list of users in the values?
const users = document.querySelector("#users");
const json = JSON.parse(users.dataset.users);
var tribute = new Tribute({
values: ** Load users this line **
});
I see that you are assigning the result of JSON.parse(users.dataset.users) to the constant "json". This leads me to think you may misunderstand the resulting value from JSON.parse.
The data-set value on the div is currently json, so document.querySelector("#users") will return the json value.
JSON.parse(users.dataset.users) will then convert the json (users.dataset.users) into a JavaScript value, in this case returning the array of users I believe you wish you assign to the values property in the Tribute constructor.
I've switched your variable names below to make this more clear.
const json = document.querySelector("#users");
const users = JSON.parse(json.dataset.users);
let tribute = new Tribute({ values: users });
* As "the_previ" pointed out, without the definition for Tribute it's unclear to us what value the "values" property expects (ie. String, Number, Array). I've assumed you're looking to pass in the array of users.
It's actually very simple using Lodash!
You just need to import Lodash, and use the map function:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script>
const users = document.querySelector("#users");
var json = JSON.parse(users.dataset.users);
const userlist = (_.map(json, "name"));
</script>
userlist will be an array containing every "name" value.
If you want to use userName values instead, just replace name with userName on the map function!
How do I get all the different key/value pairs stored in the localstorage whenever my window reloads using vanilla JS.And also How to store these data in a object with similar properties which I receive from the localStorage ?
for eg. :
if I have thses 2 things in local storage
localStorage.setItem("todoList","Task 1");
localStorage.setItem("doingList","Task 1");
You can only put one value in a localStorage key.
Put the values in an array, convert the array to JSON, and store that.
var todoList = ["Task 1","Task 2","Task 3"];
localStorage.setItem("todoList", JSON.stringify(todoList));
To retrieve it you call JSON.parse()
todoList = JSON.parse(localStorage.getItem("todoList") || "[]");
Just destruct the localStorage and get all Items
const items = { ...localStorage };
console.log(items)
I want to update all the fields in a MongoDB document and I have a Javascript object that contains all these fields. I could easily type out each field to update but this seems like a lot of manual work and not reusable. I wanted to do something like below but this creates an object containing all the new field data within the document called newData.
I've tried JSON.stringify on the variable but the format isn't appropriate for update.
var newData = {
_id:ObjectId("53245234..."),
id: 88888,
firstData: "someData",
secondData: 787855,
thirdData: [ 45,17,12,234]
};
var collection = db.get('CollectionToUpdate');
//strip out dB id so as not to overwrite it, possibly not needed
if ("_id" in newData) {
delete newData["_id"];
}
//find the correct document based on program generated id and update
collection.update({id: newData.id}, {
newData
})
If you trust newData will not have any keys you don't intend (like update operators) this should work:
var collection = db.get('CollectionToUpdate');
collection.update({id: newData.id}, newData)
Note that this replaces the document. I assume that is what you meant by "update all the fields". update does not replace "_id".
Documentation for update
I have an object containing multiple key value pairs, I want to add all the keys and their values, from inside the object to an existing node, without disturbing the data already present inside the node.
If i write like this
var ref = firebase.database().ref("hams/spam_words/");
ref.update({
new_words_ham //new_word_ham is an object containing n number of words
});
it will add new_words_ham as another child node inside the main node , i cannot have that
even using a forloop on the object does not work
var ref = firebase.database().ref("hams/spam_words/");
for(var i in new_words_ham){
var word = i
ref.update({
i
});
I am new to js as well as to firebase. Please do tell me if i have got any concept wrong
Your existing code
//new_word_ham is an object containing n number of words
firebase.database().ref("hams/spam_words/").update({
new_words_ham
});
Can be rewritten as
firebase.database().ref("hams/spam_words/").update({
new_words_ham: new_words_ham
});
when the shorthand syntax is expanded. What I believe you want is simply
firebase.database().ref("hams/spam_words/").update(new_words_ham);