Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用firebase云功能删除多个文档_Javascript_Node.js_Firebase_Google Cloud Functions - Fatal编程技术网

Javascript 使用firebase云功能删除多个文档

Javascript 使用firebase云功能删除多个文档,javascript,node.js,firebase,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Functions,很抱歉问你这个菜鸟问题,我正要扔掉我的笔记本电脑 我是js新手,一直在努力理解如何使用承诺 删除会话时,会触发此函数,并应循环抛出会话中包含的所有消息并将其删除 我的问题是我不知道在哪里删除这些消息,也不知道如何删除。 如何删除邮件 exports.deleteMessages = functions.firestore .document('users/{userId}/conversations/{conversationId}') .onDelete(event => {

很抱歉问你这个菜鸟问题,我正要扔掉我的笔记本电脑

我是js新手,一直在努力理解如何使用承诺

删除会话时,会触发此函数,并应循环抛出会话中包含的所有消息并将其删除

我的问题是我不知道在哪里删除这些消息,也不知道如何删除。 如何删除邮件

exports.deleteMessages = functions.firestore
.document('users/{userId}/conversations/{conversationId}')
.onDelete(event => {

    // Get an object representing the document prior to deletion
    const deletedConversation = event.data.previous.data();

    return database.collection('messages')
        .where('conversationId', '==', deletedConversation.id).get()
        .then(snapshot => {

            snapshot.forEach(document => {
                const data = document.data();
                database.collection('messages').document(data.id).delete();
            });

            return console.log("Don't even no why I'm returning this")
        })
        .catch(error => {
            console.log('Error when getting document ' + error)
        });
});

必须使用Promise.all(),它“返回一个承诺,当iterable参数中的所有承诺都已解决或iterable参数不包含承诺时,该承诺将解决。”

你应该遵循以下原则:

const promises = [];

return database.collection('messages')
    .where('conversationId', '==', deletedConversation.id).get()
    .then(snapshot => {

        snapshot.forEach(document => {
            //Create a Promise that deletes this document
            //Push the Promise in the "promises" array
            promises.push(deleteDocPromise(document))

        });
        //and return: 
        return Promise.all(promises);
    })
    .then(
      //Do whatever you want in case of succesfull deletion of all the doc
    )
    .catch(error => {
        ....
    });
要创建删除承诺,请执行以下操作

function deleteDocPromise(document) {
        //using the code of your question here
        const data = document.data();
        return database.collection('messages').doc(data.id).delete();   
}

多谢各位!我的代码中存在类型错误,因此返回database.collection('messages').document(data.id).delete();应为return database.collection('messages').doc(data.id).delete();,这是我的错,除了它的工作正如预期@user3116871 Cool:-)我已经用database.collection('messages').doc(data.id.delete()修改了响应;