Reputation: 1
I have an Ionic-Angular Project. I'm trying to develop something like a feed where you can post updates (like in Facebook).
I use a Firebase backend and I want to store the users post in this structure: Posts/uid/post_id/[POST DATAS]
,
but I don't know how to do something like auto_increment in mysql for the post_id.
createPost() {
this.post.uid = firebase.auth().currentUser.uid;
this.post.created_at = Date.now();
this.post.comments = null;
this.post.dislikes = 0;
this.post.likes = 0;
this.afDatabase.object(`post/${this.post.uid}/${--HERE I WANT TO SET THE POST ID--}`).set(this.post);
this.postSuccessfulAlert();
}
Upvotes: 0
Views: 104
Reputation: 1274
but I don't know how to do something like auto_increment in mysql for the post_id.
Firebase/Firestore does not have an auto increment feature, as far as I know. If you are creating a new document, firebase will automatically assign a new unique id for that document unless you specify the id to use.
If you want the generated document id ahead of time (because you are using the set
method) then you can assign a docRef and grab the id.
example:
const newDoc = firestoreInstance.collection(`post/${this.post.uid}/posts).doc();
const newDocId = newDoc.id;
Firebase uses a Firebase Timestamp
for its dates whereas you are setting a js date to this.post.created_at
You should instead use a Firebase Timestamp:
const serverTimestamp = firestoreAdmin.firestore.Timestamp.now();
Upvotes: 1