简体   繁体   中英

Firebase firestore remove item from array of field

I am using Firebase Firestore to make my website

在此处输入图像描述

As you can see, I have a field commentList and its type is array and now I want to remove the first item of the array This is what I did:

exports.delete__comment = (req, res, next) => {
  const {id} = req.params;
  const {comment} = req.body;

  return db
    .collection('instagram__posts')
    .doc(id)
    .update({
      commentList: FieldValue.arrayRemove(JSON.stringify(comment)),
    })
    .then((doc) => {
      console.log('comment deleted', comment);
      next();
    })
    .catch((err) => {
      res.status(500).json({err});
      console.log(err);
    });
};

if the item was deleted, I will console.log('comment deleted', comment); and it worked

在此处输入图像描述

but the sad thing is it does not remove in the database for me instead, it still there. Please show me how to fix it, thank you so much and Merry Chirstmas

I am not sure why you converted comment object to string using JSON.stringify .

Try to use comment instead of JSON.stringify(comment) .

I am using Firebase Firestore to make my website

在此处输入图像描述

As you can see, I have a field commentList and its type is array and now I want to remove the first item of the array This is what I did:

exports.delete__comment = (req, res, next) => {
  const {id} = req.params;
  const {comment} = req.body;

  return db
    .collection('instagram__posts')
    .doc(id)
    .update({
      commentList: FieldValue.arrayRemove(JSON.stringify(comment)),
    })
    .then((doc) => {
      console.log('comment deleted', comment);
      next();
    })
    .catch((err) => {
      res.status(500).json({err});
      console.log(err);
    });
};

if the item was deleted, I will console.log('comment deleted', comment); and it worked

在此处输入图像描述

but the sad thing is it does not remove in the database for me instead, it still there. Please show me how to fix it, thank you so much and Merry Chirstmas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM