Reputation: 4243
I'm newbie with Firebase and I want use Firestore to store the user register content. I've a collection called 'users', and each user have a 'register' that must contains a JSON Document and a 'monthlies' that must be a collection or a array of documents. At the moment I tried just add the 'register' document with two fields:
async saveUser(user) {
var db = firebase.firestore();
await db
.collection("users")
.doc(user.uid)
.doc("register")
.set(
{
name: user.displayName,
email: user.email
},
{
merge: true
}
);
},
but the error is shows:
Uncaught (in promise) TypeError: db.collection(...).doc(...).doc is not a function
at _callee$ (Login.vue?7463:75)
at tryCatch (runtime.js?96cf:45)
at Generator.invoke [as _invoke] (runtime.js?96cf:271)
at Generator.prototype.<computed> [as next] (runtime.js?96cf:97)
at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
at _next (asyncToGenerator.js?1da1:25)
at eval (asyncToGenerator.js?1da1:32)
at new Promise (<anonymous>)
at VueComponent.eval (asyncToGenerator.js?1da1:21)
at VueComponent.saveUser (Login.vue?7463:75)
So, How I can create this structure and update easyest?
Upvotes: 1
Views: 650
Reputation: 598901
As Doug answered, you can store those values in a nested collection.
But if the values are singular (they're not a collection), I'd typically store them directly in the user document. So:
async saveUser(user) {
var db = firebase.firestore();
await db
.collection("users")
.doc(user.uid)
.set(
{
name: user.displayName,
email: user.email
},
{
merge: true
}
);
},
If you are already storing other data for the user, you can store the new data as nested fields in that same document with:
async saveUser(user) {
var db = firebase.firestore();
await db
.collection("users")
.doc(user.uid)
.set({
register: {
"name": user.displayName,
"email": user.email
}}, {
merge: true
}
);
},
Upvotes: 1
Reputation: 317467
With Firestore, documents can't contain immediately nested documents. If you want to organize a document under another document, you will need to choose a name of a subcollection, then use that to build documents nested in that subcollection:
await db
.collection("users")
.doc(user.uid)
.collection("data")
.doc("register")
.set(...)
Here, I just chose the name "data" for a subcollection. You should organize your data in a way that makes sense for the queries you intend to perform.
Upvotes: 3