这是代码
loginController.prototype.newUserValidation = function(name,email,username,password, callback){
var errorMessage = {};
var name = validator.trim(name);
var email =validator.trim(email);
var username = validator.trim(username);
var password = validator.trim(password);
if(!validator.isLength(name,1)){
errorMessage.push("Name is required field");
}
if(!validator.isLength(email,1)){
errorMessage.push("Email is required field");
} else if(!validator.isEmail(email)){
errorMessage.push("Email is not valid");
}
if(!validator.isLength(username,1)){
errorMessage.push("username is required field");
}
if(!validator.isLength(password,1)){
errorMessage.push("password is required field");
}
callback(errorMessage);
}
此错误即将发生:
对象#没有方法'推送'
TypeError: Object #<Object> has no method 'push'
at Database.loginController.newUserValidation (/home/vinove/Desktop/AshutoshJha/controller/login-controller.js:49:16)
at module.exports (/home/vinove/Desktop/AshutoshJha/routes/index.js:166:14)
at Layer.handle [as handle_request] (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/layer.js:95:5)
at next (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/layer.js:95:5)
at /home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/index.js:330:12)
at next (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/home/vinove/Desktop/AshutoshJha/node_modules/express/lib/router/index.js:176:3)
答案 0 :(得分:0)
这是一个对象:
var errorMessage = {};
对象是一组键/值对:
{
"name": "john",
"age" : 21
}
如果您希望能够将随机数据添加到变量中,则需要一个数组:
var errorMessage = [];
然后你的代码就可以了
答案 1 :(得分:0)
push方法适用于数组数据类型。
在你的代码中你定义了json
即var errorMessage={}
。
内部是json。
这就是为什么它显示错误Object # has no method 'push'
所以也只需更改
即可摆脱此错误var errorMessage = {};
到
var errorMessage = [];