I want to update an item in my application so I need an id that Firebase creates:
https://db.firebaseio.com/userid/books/-L6_e2KsseGzwM8a0YUv
The problem is that Firebase returns an object of objects, so I returned Object.values(data) and now I don't have that id which I could maybe access if Firebase returns classic array..
you will get the key of the object by object.key. and for value you can do object.val()
For multiple object you can try Object.keys() and Object.values()
Is this you wanted? I can be more specific if you can post the block of code.
Related
I have the following data from an API
Link from data from de api
I need to access into product_last_data but I don't know how to access because of the element '_source' with the underscore at the beginning
I've been trying the following:
response.data.data (This works fine)
But when I try to access to any element with the underscore I don't know how to do it I've been trying this:
response.data.data['_source']
response.data.data is an array of objects, so you need to iterate the array (or pick a single element) to access the objects underneath. For example:
response.data.data.forEach(o => console.log(o._source.product_last_data))
or
console.log(response.data.data[0]._source.product_last_data)
Note that _source is a valid key which does not require [] notation to access.
I'm trying to turn an array in which every element is an array containing the key, and the value(like entries), I'm trying to convert it back to an object element by the help of the method Object.fromEntries(array) but there seem to be a problem.
This is the error I get: TypeError: Object.fromEntries requires the first iterable parameter yields objects
Here's how I use the method:
const saved = JSON.parse(Object.fromEntries(localStorage.getItem(key)))
The local storage value it should retrieve:
[["$$typeof",null],["type",null],["key",null],["ref",null],["props",{"data":"a data containing collections of objects(fetched from an API)"}]
Since it might be helpful, what I'm trying to do is to store a react component(yes, I work on ReactJS) to local storage and get it back when necessary.
If you don't think that this portion of code is not the reason why this issue appears, please do let me know.
Suppose this scenario.
I have a collection where I store products, this products have a property called "props" and this is an array of objects, that have properties like measure, weight.
The problem comes when I'm trying to insert new products. I'm receiving an array of objects that will be inserted, but each element of the array could have a property called "propsReference", and If it's there, it would be an ID that refers to the object that's currently inserted and have some props
So, if it has the propsReference property with an ID, I want to copy the properties of this object to the object I'm going to insert
The only solution I can think of is doing a foreach to the first array, if it has the propsReference do a query, get the props and insert it into this element of the array as a new property of the object.
But I'm having a lot of problems with doing queries inside a for loop, there's any way in mongoose or something to avoid that?
Thanks in advance
I have a firestore firebase database , in which I have a collection users
there is an array in the collection and in the array there is a map
in map there is a field qty.. I want to increment that qty value..
using increment doesnt help as the qty is inside a array index
db.collection("users").doc(checkId).update({
myCart: firebase.firestore.FieldValue.arrayUnion({
qty: firebase.firestore.FieldValue.increment(1),
}),
this is the error Output =>
Uncaught (in promise) FirebaseError: Function FieldValue.arrayUnion() called with invalid data. FieldValue.increment() can only be used with update() and set()
My answer below won't work, given that the qty is in an array. The only way to update an item in an array is to read the entire document, update the item in the array, and then write the entire array with the updated item back to the document.
An alternative would be to use a map instead of an array, and then update the qty using the approach outlined in my (old, and non-working) answer below 👇
You need to specify the full path to the field you're trying to update. So I think in your case, that'll be:
db.collection("users").doc(checkId).update({
"myCart.0.qty": firebase.firestore.FieldValue.increment(1)
}),
The field you want to update is embedded in an array. In this case, you can't use FieldValue.increment(), since it's not possible to call out an array element as a named field value.
What you'll have to do instead is read the entire document, modify the field in memory to contain what you want, and update the field back into the document. Also consider using a transaction for this if you need to update to be atomic.
(If the field wasn't part of an array, you could use FieldValue.increment().)
As of today (29-04-2020)... this is tested by me.
Suppose my data structure is like this:
collection: Users
Any document: say jdfhjksdhfw
It has a map like below
map name: UserPageVisits
map fields: field1,field2,field3 etc
Now we can increment the number field in the map like below:
mapname.field1 etc...
That is use the dot operator to access the fields inside the map just like you would do to an object of javascript.
JAVA Code (Android), update the field using transactions so they can complete atomically.
transaction.update(<documentreference object>,"UserPageVisits.field1",FieldValue.increment(1));
I have just pushed a version of my app which uses this concept and it's working.
Kudos !!
My Best Regards
Previous answers helped me as well, but dont forget about the "merge" property!!! Otherwise it will overwrite your entire array, losing other fields.
var myIndex = 0;
const userRef = db.collection('users').doc(checkId);
return userRef.update({
'myCart.${myIndex}.qty': admin.firestore.FieldValue.increment(1)
}, {
merge: true
});
Im kinda new in firebase. As a first step, I would like to set a post and get calls to my newly made database in firebase.
Firebase has a lot of native functions which allows me to get elements and push them to the db. However, I would prefer to use native get/post calls.
My post calls seem to work just fine, it properly stores an element in the db and returns 200 status.
When I enter to my DB in firebase, I can see that it posses following data:
entries:
-L1cxn3-rLgp7PsPwjV3
author: "Mark"
title: "Hello"
-L1cyaOQ4TUYd3m16VfT
autor: "Lily"
title: "Hi"
So as I said before, it stores correct data. But the structure is unknown for me. It's like a map or an object.
I would like to ask you for help, how to properly retrieve it from get call.
The get call returns:
{"-L1cxn3-rLgp7PsPwjV3":{"author":"Mark","title":"Hello"},"-L1cyaOQ4TUYd3m16VfT":{"author":"Lily","title":"Hi"}};
I could take all keys from it Object.keys(data)
Then iterate over it to get it as an array -
Object.keys(data).map(r => data[r])
Now I would have an array of objects.
Is it a proper way to deal with it? Should I stay with my get/post calls or I should rather use firebase built-in functions? Thank u in advance! :)
Just use Object.values() if you want the array of objects
console.log(Object.values({"-L1cxn3-rLgp7PsPwjV3":{"author":"Mark","title":"Hello"},"-L1cyaOQ4TUYd3m16VfT":{"author":"Lily","title":"Hi"}}))