我试图在FamilyData的回调函数中插入FamilyData对象和UserData中的同一对象。但是在这里,我能够向FamilyData插入一个对象,但是在我得到的回调函数对象中为null。这导致异步回调异常。在控制台中,我到了带有字符串化对象的“用户ID存在”,但此后“ FamilyId”没有打印。那里显示空对象。 我在下面附加的代码:
`
registerNewUser': function (obj) {
let result=null;
result = {};
let UserData1;
try {
let userId;
let messageArray;
let FamilyEmail;
let FamilyId1;
messageArray = Meteor.Validation.Registration(obj);
if (messageArray.length > 0) {
let msg = Meteor.Utility.appendMessages(messageArray);
result.Success = false;
result.Message = msg;
} else {
result.Success = true;
result.Message = 'User added successfully';
if (!obj.check_familymember) {
userId = Accounts.createUser({
email: obj.email,
password: obj.pwd
});
if (userId != undefined) {
console.log("user id present",JSON.stringify(obj));
FamilyData.insert({
FamilyName: obj.FamilyName,
Address: obj.Address,
CreatedBy: obj.firstname,
Latitude: obj.latitude,
Longitude: obj.longitude
}, function (err, FamilyId) {
console.log("FamilyId",JSON.stringify(obj));
UserData.insert({
FirstName: obj.firstname,
LastName: obj.lastname,
Gender: obj.genderval,
DateOfBirth: obj.dob,
Email: obj.email,
Password: obj.pwd,
FamilyId: FamilyId,
UserId: userId,
});
if(err){
// throw err;
result.Message=err;
console.log("error is",err);
}
});
console.log("user created");
} else {
result.Success = false;
result.Message = 'Unable to create user ';
}
} else {
FamilyEmail = obj.FamilyEmailId;
UserData1 = UserData.findOne({
Email: FamilyEmail
});
if (UserData1 == null) {
result.Success = false;
result.Message = 'FamilyId does not exist..';
} else {
FamilyId1 = UserData1.FamilyId;
userId = Accounts.createUser({
email: obj.email,
password: obj.pwd
});
if (userId != undefined) {
UserData.insert({
FirstName: obj.firstname,
LastName: obj.lastname,
Gender: obj.genderval,
DateOfBirth: obj.dob,
Email: obj.email,
Password: obj.pwd,
FamilyId: FamilyId1,
UserId: userId
});
} else {
result.Success = false;
result.Message = 'Unable to create user ';
}
}
}
}
} catch (e) {
logError(e);
result.Success = false;
result.Message = e.message;
}
`
答案 0 :(得分:0)
流星使用纤维运行异步代码
因此您不必在数据库操作中使用回调
尝试以下
let familyId = FamilyData.insert({
FamilyName: obj.FamilyName,
Address: obj.Address,
CreatedBy: obj.firstname,
Latitude: obj.latitude,
Longitude: obj.longitude
});