简体   繁体   中英

How to delete a field in a collection with firebase-admin?

I have a collection like this into firebase realtime database:

在此处输入图像描述

I need to delete the first element (the one that finishes with Wt6J ) from server side using firebase-admin .

I have this simple code:

const deleteNotification = () => {
  const key1 = 'FhN6Ntw8gyPLwJYVzcHy0E8Wq5z2';
  const key2 = '-MzGhZ2psGLivIfTWt6J';
  const notsRef = db.ref(`notifications/${key1}/${key2}`);
  notsRef.remove();
};

This doesn't work. What method should I use to delete a specific field? How do you think I can do it?

I would think to use await in a try catch block. Await starts another thread which returns once it has completed. As people said above - your cloud function is likely being killed before the remove actually happens.

const deleteNotification = async () => {
  try{
    const key1 = 'FhN6Ntw8gyPLwJYVzcHy0E8Wq5z2';
    const key2 = '-MzGhZ2psGLivIfTWt6J';
    const notsRef = db.ref(`notifications/${key1}/${key2}`);
    await notsRef.remove();
  } catch( err ) {
    console.log( 'failed to remove record.' );
    console.log( err );
  }
  console.log( 'removed record successfully.' );
};

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