错误状态后,Firebase路由仍然继续

因此,我一直在观看有关Firebase的教程,并且正在创建一个像应用程序这样的简单Twitter。

具有data['text'][1]screams的架构

在我的createScreamComment中,我的问题是,在我确认没有comments文档之后,该路由将发送与该路由相同的 404 状态。但由于scream

,它还会继续触发其他回报,从而触发我的catch error

exports.commentOnScream

[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
tooy12345 回答:错误状态后,Firebase路由仍然继续

所有then回调都将按照彼此链接的顺序被调用。这种行为是正常的,并且只是承诺链工作的方式。通过Express发送响应不会终止链。

您可能要考虑将仅当在文档中找到合适的条件块时才发生的逻辑转移。例如,简短地:

.then(doc => {
    if (!doc.exists) {
        res.status(404).json({ error: 'Scream not found' });
    }
    else {
        return doc.ref.update(...)
        .then(() => {
            return db.collection('comments').add(newComment);
        })
        .then(() => {
            res.json(newComment);
        })
    }
})

或者,如果您采用async / await语法,则这样的事情会变得更容易,这样您就不会以那么多的嵌套回调结束。

本文链接:https://www.f2er.com/3133840.html

大家都在问