Firebase函数HTTP请求检查正文中的空值

问题描述 投票:-1回答:1

我遇到问题,我到处搜索,找不到解决方案。

我正在Firebase Functions中构建一种API,我正在尝试检查POST请求的正文中是否提供了我需要的所有值。问题是,虽然我可以检查所需的参数是否在主体中,但我无法检查其值是否为空。

示例1:我在主体中需要一个名为EntityID的参数,我可以检查主体是否包含该参数,但不能检查其是否分配了任何值:如果body是{},我可以看到我没有提供的EntityID,但是如果它是{EntityID:''},我不知道。

示例2:如果执行此操作:console.log(data.EntityType)如果请求的正文中未提供该值但该参数存在,则作为响应返回null但是,如果我这样做:console.log(data.EntityType == null)我得到的是假。

感谢您提供任何帮助!

相关代码:

//Doesn't work
if (data.EntityType == null || data.EntityID == null || data.AppID == null) {
        console.log("API called with invalid parameter values");
        res.status(400).send("API called with invalid parameter values");
        return;
      }
javascript node.js firebase google-cloud-functions api-design
1个回答
0
投票

如果这些属性为Strings,则可以使用“ ===”运算符进行检查,例如:

if (data.EntityType === "" || data.EntityID === "" || data.AppID === "") {
    console.log("API called with invalid parameter values");
    res.status(400).send("API called with invalid parameter values");
    return;
}
© www.soinside.com 2019 - 2024. All rights reserved.