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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    [Function DocumentReference.set() called with invalid data](https://stackoverflow.com/questions/48156234/function-documentreference-set-called-with-invalid-data-unsupported-field-val) – HDJEMAI Mar 03 '18 at 14:32
  • Shouldn't that be `activityId: act.id` instead of `activityId = act.id`? – David Knipe Mar 03 '18 at 20:25

1 Answers1

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
});
Hareesh
  • 6,770
  • 4
  • 33
  • 60