首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MCS/nodejs中的URI参数验证

MCS/nodejs中的URI参数验证
EN

Stack Overflow用户
提问于 2017-01-30 18:31:32
回答 2查看 415关注 0票数 1

需要你的帮助来解决一个问题。我在MCS中编写了一个自定义API,方法类型为"GET“,将参数与URI一起传递。

代码语言:javascript
复制
service.get('/mobile/custom/****/deviceVersion/:deviceType',function(req,res){
    var reqParams = req.params;
    var finalResponse;
    var params='/'+reqParams.deviceType;
    console.info("Request Params>"+params);
    if(reqParams.deviceType=='{}'){     // ***Here is my problem***
        finalResponse = jbuilder.encode(function (json) {
                     json.set('Response', function (json) {
                          json.set('responseCode', '400');
                          json.set('responseMessage', 'Malformed request query');
                     });

                });
            res.status(400).send(finalResponse);
            res.end();

    }else{
        //console.info("In Else and length=>"+reqParams.deviceType+"//"+reqParams.deviceType.length);
        //var params='/'+reqParams.deviceType;
        var connector='/deviceVersion';
        commonHandler.CommonHandlerGetMethodFunction(req,res,connector,params);
    }

});

在我的例子中,我必须检查参数deviceType是否为空。

我尝试了以下方法

代码语言:javascript
复制
 1. if(reqParams.deviceType=='{}'){}

 2. if(JSON.Stringify(reqParams.deviceType)=='{}'){}

 3. if(JSON.Stringify(reqParams.deviceType).length==0){}

有人能告诉我针对null的验证的正确方法吗?提前感谢

已更新

在将deviceType作为null传递时,我得到的错误是

代码语言:javascript
复制
{
    "type": "w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
    "status": 404,
    "title": "API not found",
    "detail": "We cannot find the API ******/1.0 for the provided URL path /deviceVersion/. Verify your request parameters and try again.",
    "o:ecid": "005Hp2YhoPF3j4C_nDs1yZ000Uba00001w, 0:3",
    "o:errorCode": "MOBILE-57945",
    "o:errorPath": "/mobile/custom/******/deviceVersion/"
 }

MCS自定义Api中给定的终结点为/deviceVersion/{deviceType}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-16 09:13:46

这个问题被交叉发布到Oracle OTN MCS论坛上,并在那里得到了回答:https://community.oracle.com/thread/4012301

票数 1
EN

Stack Overflow用户

发布于 2017-01-30 18:38:50

if (!req.params.deviceType)应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41933674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档