我有一个客户端函数,用于注册将数据对象传递给服务器以创建用户的用户。我在客户端完全看到了数据对象,但是当服务器函数运行时(registerNewUser
),它失败并显示数据未定义的错误。
在客户端上, register.js :
if (Meteor.isClient){
Template.registrationStep2.events({
'submit form': function(e, template) {
e.preventDefault();
if (Session.get("registrationInstitutionID") && Session.get("registrationInstitutionRole")) {
var data = [];
var institutionID = template.find('#institutionID').value;
var institutionRole = template.find('#institutionRole').value;
var institutionName = template.find('#institutionName').value;
data.login = template.find('#login').value;
console.log("login: " + data.login);
var firstName = template.find('#firstName').value;
console.log("firstName: " + firstName);
var lastName = template.find('#lastName').value;
console.log("lastName: " + lastName);
data.email = template.find('#email').value;
console.log("email: " + data.email);
data.password = template.find('#password').value;
data.profile = {
firstName: firstName,
lastName: lastName,
role: institutionRole,
institutionID: institutionID
}
Meteor.call('registerNewUser', data, function(error, result){
Meteor.log.trace("User Data Object", data);
console.log(data);
if(error){
console.log("error", error);
Meteor.log.error("Error Creating User", error);
}
if(result){
console.log("SUCCESS adding user.");
Meteor.log.error("Success Result", result);
Session.set("flashType", "success");
Session.set("flashMessage", "Your account has been created.");
Router.go('registrationComplete');
}
});
} else {
// alert("Need institution");
$('#needInstitutionModal').modal('show');
}
}
});
}
这是 server.js :
Meteor.methods({
'registerNewUser': function(data) {
Meteor.log.debug("User Data Object on Server", data);
console.log(data);
return Accounts.createUser({
username: data.login,
email: data.email,
password: data.password,
profile: data.profile
}
});
如果我对createUser函数的值进行硬编码,则一切正常。但是当我使用数据对象时,每个字段都会出现这样的错误。 (我在调用服务器函数之前从控制台日志中知道客户端中存在的数据对象。)
以下是特定错误行,如果下面很难看到: Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username
以下是日志的相关输出:
I20150710-07:56:51.072(-5)? (07:56:51) [DEBUG @ programs/server/packages/ostrio_loggerconsole.js:67] - Object:
I20150710-07:56:51.072(-5)? {
I20150710-07:56:51.072(-5)? "time": "2015-07-10T12:56:51.070Z",
I20150710-07:56:51.072(-5)? "level": "DEBUG",
I20150710-07:56:51.072(-5)? "message": "User Data Object on Server",
I20150710-07:56:51.072(-5)? "additional": []
I20150710-07:56:51.072(-5)? }
I20150710-07:56:51.075(-5)? logging data object on server ================+
I20150710-07:56:51.075(-5)? []
I20150710-07:56:51.077(-5)? Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username
I20150710-07:56:51.078(-5)? at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:177:13)
I20150710-07:56:51.078(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:314:9
I20150710-07:56:51.078(-5)? at Function._.each._.forEach (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/underscore.js:147:22)
I20150710-07:56:51.078(-5)? at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:308:5)
I20150710-07:56:51.078(-5)? at check (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:50:5)
I20150710-07:56:51.078(-5)? at createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:795:3)
I20150710-07:56:51.078(-5)? at Object.Accounts.createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:875:10)
I20150710-07:56:51.078(-5)? at [object Object].Meteor.methods.registerNewUser (app/server/server.js:17:25)
I20150710-07:56:51.078(-5)? at [object Object].methodMap.(anonymous function) (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2536:30)
I20150710-07:56:51.078(-5)? at maybeAuditArgumentChecks (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:2445:12)
I20150710-07:56:51.078(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1476:20
I20150710-07:56:51.078(-5)? at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150710-07:56:51.079(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1475:41
I20150710-07:56:51.079(-5)? at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150710-07:56:51.079(-5)? at [object Object]._.extend.protocol_handlers.method (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1474:51)
I20150710-07:56:51.079(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2425:38
I20150710-07:56:51.079(-5)? Sanitized and reported to the client as: Match failed [400]
非常感谢你的帮助。
答案 0 :(得分:2)
我不确定为什么在将数据用作对象时将数据声明为数组。
尝试
var data = {};
代替。
我只是尝试自己创建一个准系统版本,当我将它声明为一个有趣的数组时,我遇到了同样的问题。
在客户端上,一切看起来都很正常(一旦开始使用密钥,它就放弃了数据)但在服务器上,只要涉及数据就是一个空数组。奇怪的结果,但声明数据作为对象修复它。