错误的请求错误400 firebase但功能正常

时间:2020-11-03 09:37:27

标签: node.js firebase rest google-cloud-firestore google-cloud-functions

我正在尝试在我的REST API中创建一个函数,以更改存储在Firebase Cloud Firestore中的文档中的值。我已经编写了此函数并与邮递员进行了测试:

index.js

const express = require('express');
const app = express();

app.get('/unsubNewsletter/:userId', newsletterStop);

user.js

exports.newsletterStop = (req,res) =>{
    const document = db.doc(`/users/${req.params.userId}`);
    document
        .get()
        .then(doc =>{
            if(!doc.exists){
                return res.status(404).json({error: 'User not found'})
            } else {
                document.update({newsletter:false})
            }
        })
        .catch(err => {
            console.error(err);
        });
};

目标文档中的值已更改,但邮递员向我显示此错误:

<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>

该如何解决?

如果控制文档是否存在的构造不检查,则为PS

1 个答案:

答案 0 :(得分:1)

根据the documentation you should always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might continue to run and be forcibly terminated by the system

添加类似内容:

document.update({newsletter:false})
.then(() => {
  res.status(200).send('Done!');
})