javascript - 什么会阻止 sailsjs 识别 req.params ?

标签 javascript json node.js sails.js

我刚刚解决了一个奇怪的错误。我正在发送带有有效 json 和 application/json header 的 REST PUT 调用。

PUT 调用中的 json 为

{
    "gravatarURL":     "http://www.gravatar.com/avatar/?d=mm"
}

关于为什么 req.params 中无法识别有效 json 的任何想法吗?

处理解决方法的代码将 json 正文解析为参数是

updateProfile: function(req, res) { 

    tag = 'UserController.updateProfile' ;

    user = {} ;
    user.id = req.session.userId ;
    if (!user.id){
        user.id = 0 ;
    }

    /* the following line of code should result in params with elements */
    params = req.params ;
    /* what I get is params == [] */

    /* this is the start of the workaround */
    if ( params.length == 0) {
        try {
            /* copying the body creates a valid json object */
            params = {}
            params.body = req.body ;
            params = params.body ;
            /* gravatarURL is the parameter sent in via the REST PUT call */ 
            user.gravatarURL = params.gravatarURL ;
        } catch (e){
            /* ignore and pass through to error with no params */
            console.log( tag + '.error: ' + e.message ) ;
        }
    } else {
        /* this is what I expect to be able to do */
        user.gravatarURL = req.param['gravatarURL'] ;
    }

    if ( user.id && user.gravatarURL) {
        console.log( tag + '.update.start' ) ;
        User.update({ id: user.id }, { gravatarURL: user.gravatarURL }, function(error, updatedUser) { 
            if (error) {
                console.log( tag + '.update.error: ' + error.message ) ;
                return res.negotiate(error); 
            } else {
                console.log( tag + '.update.finish: ' ) ;
                return res.json(updatedUser);   
            }
        });     
    } else {
        if ( !user.id ) {
            error = {} ;
            error.message = 'authorisation required. please login.' ;
            return res.badRequest({error:error}) ;
        }
        if ( !user.gravatarURL ) {
            error = {} ;
            error.message = 'gravatarURL: required' ;
            return res.badRequest({error:error}) ;
        }
    }

} ,

最佳答案

取决于您如何进行调用,将取决于参数在请求对象 (req) 上显示的位置。

由于您要发送带有 application/json header 的 JSON 对象,因此该对象将添加到 req.body 中。带有 URL 路径的请求将被添加到 req.params 中。根据 documentation req.params 是“包含从 URL 路径解析的参数值的对象”。例如,如果您有路由/user/:name,则 URL 路径中的“name”将作为 req.params.name 提供。该对象默认为 {}。'

因此,如果您想通过 req.params 访问参数,您可以将请求更改为“/updateProfile?gravatarURL=www.someurl.com”,否则如果您传递 JSON 对象,它将在 req.body 中可用。因此,您的解决方法是多余的,因为按照设计,您想要访问的内容已经安全地位于 req.body 中。

文档并没有很好地解释这一点,但是通过一些尝试和错误,很容易找出传递的值显示在请求对象上的位置。

关于javascript - 什么会阻止 sailsjs 识别 req.params ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664371/

相关文章:

javascript - Protractor 页面对象是否应该公开 ElementFinder

python - JSONField 对于 POST 序列化为 json,对于 GET 序列化为字符串

javascript - 如何将 json_encode 返回的对象转换为 javascript 变量

javascript - jQuery 中的弹性文本区域插件

javascript - 使用 javascript/jquery 从 API 获取 Json 响应

javascript - 从addEventListener()调用函数

java - 努力将 JSON 负载映射到模型类

javascript - Gruntfile 任务未正常运行

ios - 当键为特定值时运行 firebase 云函数

javascript - 使用对象的属性来对象的对象数组