Skip to content
Advertisement

React Native Firestore remove object in array

I cannot delete object inside my array of object in a document from Firestore.

Here my Document enter image description here

I have tried that

const obj = product;
    const user_post = await this.checkOwnerPost();

    console.log(obj);
    
        if (user_post){
  await this.firestore
  .collection('testCollection')
  .doc(user_post.doc_id)
  .update({
    products: firebase.firestore.FieldValue.arrayRemove(obj)
    .then(() => {
    console.log("Document successfully updated!");
    return true;
  }).catch((error) => {
    console.log(error);
    return false;
  })
});

}

Console.log(obj) = {
  "description": "1",
  "key": 1,
  "name": "first",
  "picture": "https://firebasestorage.googleapis.com/v0/b/url",
  "price": 1,
}

Here the error msg:

[Unhandled promise rejection: TypeError: undefined is not a function (near '..._firebase.default.firestore.FieldValue.arrayRemove(obj).then...')]
at node_modules/lodash/lodash.js:10487:35 in debounced
at [native code]:null in flushedQueue
at [native code]:null in callFunctionReturnFlushedQueue

Someone can help me ? Or do you have any ideas how can i do for delete and update a object inside an array ?

EDIT: Is fix, it was bad formating of JS code

Advertisement

Answer

Fix

I was a bad formating of the JS Code.

The working code is:

if (user_post){
      await this.firestore
      .collection('testCollection')
      .doc(user_post.doc_id)
      .update({products: firebase.firestore.FieldValue.arrayRemove(obj)})
      .then(() => {
        console.log("Document successfully updated!");
        return true;
      }).catch((error) => {
        console.log(error);
        return false;
      });
    };
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement