Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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 如何知道查询中缺少哪些参数?_Javascript_Node.js_Api_Express - Fatal编程技术网

Javascript 如何知道查询中缺少哪些参数?

Javascript 如何知道查询中缺少哪些参数?,javascript,node.js,api,express,Javascript,Node.js,Api,Express,我正在NodeJS建立一个预订路由器,我有很多参数。 现在,当我忘记参数时,我返回一个错误,如: 500:需要更多信息吗 我想知道当我返回错误代码时,是否有可能知道哪些参数丢失了 这是针对NodeJS中的一个新API 下面是我想从前面检索的参数(用ReactJS制作) 这是我的承诺: cdsJson.booksource(请求正文) 。然后((响应)=>{ 如果(响应!==null){ 回复={ …回应 } }如果(response.hotel.length==0){ res.respStatu

我正在NodeJS建立一个预订路由器,我有很多参数。 现在,当我忘记参数时,我返回一个错误,如:

500:需要更多信息吗

我想知道当我返回错误代码时,是否有可能知道哪些参数丢失了

这是针对NodeJS中的一个新API

下面是我想从前面检索的参数(用ReactJS制作)

这是我的承诺:

cdsJson.booksource(请求正文)
。然后((响应)=>{
如果(响应!==null){
回复={
…回应
}
}如果(response.hotel.length==0){
res.respStatus=500
回复={
sendMsg:“需要更多信息”
}
下一条(“路线”)
}
返回响应
})
如果请求成功,我将获得预订ID,否则我将获得:


错误500:需要更多信息请阅读文档或源代码


真的。如果API响应没有在错误消息中告诉您,那么就无法通过编程知道它需要哪些参数。

请尝试使用
获取。。。在
循环中,如下所示:

cdsJson.bookResource(req.body)
.then((response) => {
    if (response !== null) {
        res.response = {
            ...response
        }
    } if (response.hotel.length === 0) {
        res.respStatus = 500

        let errorStr = "Need more informations"

        for(var key in req.body) { // Get all parameters that are not set
            if(objects[key] == undefined)
                errorStr += "\nParameter ["+key+"] is missing!"
        }

        res.response = {
            sendMsg: errorStr
        }
        next('route')
    }
    return response
})

您正在尝试执行服务器端验证。在Node中,一个好的方法是为预期参数定义JSON模式,然后在路由处理程序中使用JSON模式验证器验证请求中发送的数据。这将帮助您确定请求是否有效,并帮助您自动生成错误消息。通常,使用工具(通过模式)声明验证比强制编写代码手动验证对象要好得多(即更简单、更易于维护)

  • JSON模式规范
  • 验证器

谢谢,我想你是对的。我将调用API管理员。我的代码中已经有一个验证器,但感谢您的指导谢谢,您的方法不起作用,因为我调用的API不是我的。您编写了
这是我的承诺
。所以我猜这是不是你的密码@那么你为什么不能改变它呢@Eythanfellousmissionsound^^,API不会返回任何缺少的内容或参数错误的信息。是的,这是真的,但您仍然可以检查您发送的内容。这样,您就可以向用户提示缺少哪些参数@埃森菲卢斯
cdsJson.bookResource(req.body)
.then((response) => {
    if (response !== null) {
        res.response = {
            ...response
        }
    } if (response.hotel.length === 0) {
        res.respStatus = 500

        let errorStr = "Need more informations"

        for(var key in req.body) { // Get all parameters that are not set
            if(objects[key] == undefined)
                errorStr += "\nParameter ["+key+"] is missing!"
        }

        res.response = {
            sendMsg: errorStr
        }
        next('route')
    }
    return response
})