1

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

2
  • Please include the error that you received in your question. It seems the query is working on nodejs. Commented Aug 11, 2021 at 6:58
  • @JMGelilio i just included the error msg in my question Commented Aug 11, 2021 at 7:14

1 Answer 1

2

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;
      });
    };
Sign up to request clarification or add additional context in comments.

Comments

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.