【问题标题】:bad request error 400 firebase but function works错误的请求错误 400 firebase 但功能有效
【发布时间】:2020-11-04 18:40:17
【问题描述】:

我正在尝试在我的 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 if 构造

【问题讨论】:

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


    【解决方案1】:

    根据the documentationyou 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!');
    })
    

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 2018-03-30
      • 2020-04-20
      • 1970-01-01
      • 2022-11-12
      • 2021-06-12
      • 1970-01-01
      • 2016-03-23
      • 2014-12-11
      相关资源
      最近更新 更多