2

I am trying to store my auto generated id into the documents field as.

  var act  =  this.afs.collection("activities").add({
  activityId = act.id,

    })
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});

but in the console there is an error occur. I am sure I am using wrong approach. Please help me how can I access auto id of my document? Any help will be appreciated.

2

1 Answer 1

1

You can use angularfire2 method .createId(). This will generate a random id locally.

let id = this.afs.createId();
this.afs.collection("activities").doc(id).set({
    activityId: id
});
Sign up to request clarification or add additional context in comments.

2 Comments

are you getting same error? can you console.log(act.id)?
sorry my fault, i didn't notice you using angularfire2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.