Reputation: 6795
I have made a function that fetches users from an endpoint, I succefully retrieved the users id, the ids are this ones
1,2,3,4,5,6,7,8,9,10
Now, I want those id's to be the document path to store the data, I have made this.
for (var i = 0; i < jsonresponse.length; i++) {
var obj = jsonresponse[i];
var docid = obj.id
db.collection("users").doc(docid).set(obj)
}
The output of the ids is ok, I have logged them out and they are working, but the document cant be generated with those numbers.
This is what I get from the console log error
Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
Edit
console.log("id",docid)
Upvotes: 2
Views: 2250
Reputation: 1074
if you have an array with ids, just do this:
jsonresponse = [1,2,3,4,5,6,7,8,9,10]
for (var i = 0; i < jsonresponse.length; i++) {
var obj = jsonresponse[i];
var docid = obj.id
db.collection('users').doc(""+docid).set(obj);
}
how are you testing that? maybe postman or something is sending the json as text
Upvotes: 3
Reputation: 30370
Perhaps you could take the following approach to writing users
data to firebase, by first obtaining a ref()
to the target document (via the document path that matches your desired pattern), and then calling .set()
on the document ref:
for (const obj of jsonresponse) {
/*
Obtain target document ref from path that is obj id, relative to collection type
*/
db.ref(`users/${ obj.id }`).set(obj);
}
Upvotes: 1