Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

javascript - Delete a field from Firestore, with a dynamic key

I am trying to delete a single field from a Document in Firestore The Key of the field is held in a variable e.g.

var userId = "random-id-1"

In the document I have a field of members structured like this:

{
  members:{
    random-id-1:true,
    random-id-2:true
  }
}

I would like to delete random-id-1:true, but keep random-id-2:true

How is this possible without getting the entire members object and writing an updated object?

I have tried this, however I get the error: Document references must have an even number of segments

and I also tried this:

db.collection('groups').doc(this.props.groupId).set({
  members: {
    [userId]: firebase.firestore.FieldValue.delete()
  }
},{merge: true})

However I get the error: Function DocumentReference.update() called with invalid data. FieldValue.delete() can only appear at the top level of your update data

Thanks for any help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...