Sailsjs:TypeError:无法读取未定义

时间:2016-04-19 23:27:28

标签: javascript angularjs sails.js

我正在使用sailsjs,我使用postman我尝试发送到帖子(http://localhost:1337/user?name=plz)(http://localhost:1337/user?name=plz)(http://localhost:1337/user?name=plz) 但是我得到了错误:

 TypeError: Cannot read property 'name' of undefined
at Object.module.exports.create (C:\Users\achraf\Desktop\crud\cruds\api\controllers\UserController.js:13:29)
at wrapper (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\lodash\index.js:3095:19)
at routeTargetFnWrapper (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:176:5)
at callbacks (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
at param (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
at pass (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5)
at nextRoute (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:100:7)
at callbacks (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:167:11)
at alwaysAllow (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\lib\hooks\policies\index.js:158:11)
at routeTargetFnWrapper (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:176:5)
at callbacks (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
at param (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
at pass (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5)
at nextRoute (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:100:7)
at callbacks (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:167:11)
at module.exports (C:\Users\achraf\AppData\Roaming\npm\node_modules\sails\lib\hooks\cors\clear-headers.js:14:3) [TypeError: Cannot read property 'name' of undefined]

UserController.js

module.exports = {

        create:function(req,res){

                var param = req.body.name;
                User.create({name :param.name}).exec(function(err,user){
                        //console.log(success);
                    if(err){

                        res.send("Error:Sorry!Something went Wrong");

                    }else{

                        return res.redirect("/user");

                    }

                });
    }   
};

routes.js

'post /user':   'UserController.create'

user.js的(模型)

module.exports = {

  attributes: {
        //_id:'p1',
        name:'STRING'
  }
};

3 个答案:

答案 0 :(得分:0)

假设req.body.name返回格式正确的数据,您需要使用param代替param.name,因为您将name属性直接设置为param 1}}变量。

这样做:

var param = req.body.name;
User.create({name : param}).exec(function(err,user){

答案 1 :(得分:0)

使用req.params.all();或在邮递员中使用邮寄方式,您的网址将为http://localhost:1337/user/create?name=xyz

我希望它能解决你的问题

module.exports = {

    create:function(req,res){

            var param = req.params.all();
            User.create({name :param.name}).exec(function(err,user){
                    //console.log(success);
                if(err){

                    res.send("Error:Sorry!Something went Wrong");

                }else{

                    return res.redirect("/user");

                }

            });
}   

};

答案 2 :(得分:-1)

你必须通过" name"使用帖子请求,作为帖子人post man request