我正在尝试实例化一个由子对象(user
,authData
和nameData
)组成的对象emailData
,现在我得到了它可以同步工作。作为练习,我也想尝试使用async
来实现这一点,但是我不知道执行此操作的正确语法。有人可以帮助我让async
通话正常吗?
这是我的模特,user.js
:
const titleEnum = ['Dr.', 'Mr.', 'Ms.', 'Mrs.', 'None'];
const suffixEnum = ['Jr.', 'Sr.','II','III','IV','V', 'Esq'];
const recoveryEnum = ['Recovery', 'Normal'];
const commEnum = ['Comm','Backup'];
var AuthSchema = new Schema (
{
dateEffective: {type: Date},
username: {type: String, required: true, max: 20, trim: true, lowercase: true},
password: {type: String, required: true, max: 20}
}
)
var NameSchema = new Schema (
{
title: {type: String, enum: titleEnum},
firstName: {type: String, required: true, max: 25, trim: true, uppercase: true},
middleName: {type: String, required: true, max: 25, trim: true, uppercase: true},
familyName: {type: String, required: true, max: 25, trim: true, uppercase: true},
suffix: {type: String, enum: suffixEnum}
// suffix: {type: String, max: 8, trim: true, uppercase: true}
}
)
var EmailSchema = new Schema (
{
emailAddress: {type: String, required: true, max: 25, trim: true, lowercase: true},
recoveryType: {type: String, enum: recoveryEnum, default: "Normal"},
commType: {type: String, enum: commEnum, default: 'Comm'}
}
)
var UserSchema = new Schema(
{
authData: [AuthSchema],
nameData: [NameSchema],
emailData: [EmailSchema]
}
)
module.exports = {
Auth: mongoose.model('Auth', AuthSchema),
Name: mongoose.model('Name', NameSchema),
Email: mongoose.model('Email', EmailSchema),
User: mongoose.model('User', UserSchema)
}
下面是创建新用户的路由上的同步回调函数。该代码现在可以正常工作,但是作为学习练习,我想使用async.parallel
来实现它:
var User = require('../models/user'),
Auth = User.Auth,
Name = User.Name,
Email= User.Email,
UserObj = User.User;
exports.user_create_get = function(req, res, next) {
var authObj = new Auth({
dateEffective: moment(Date.now(),"YYYY MM DD").format("YYYY MM DD"),
username: '',
password: ''
});
var nameObj = new Name({
title: '',
firstName: '',
middleName: '',
lastName: '',
suffix: ''
});
var emailObj = new Email({
emailAddress: '',
recoveryType: '',
commType: ''
});
var user = new UserObj({
authData: [authObj],
nameData: [nameObj],
emailData: [emailObj]
});
res.json(user);
}
这是我尝试使用async.parallel
实现以上代码的尝试。 (在res.json
调用之后,下面的代码不会填充/渲染/显示子对象中的任何字段,甚至不会为这些子对象生成任何_id
字段。在那里是父对象的_id
字段,但子对象只是空数组(长度为1,带有null
条目)。
var User = require('../models/user'),
Auth = User.Auth,
Name = User.Name,
Email= User.Email,
UserObj = User.User;
exports.user_create_get = function(req, res, next) {
async.parallel({
auth: function(callback){
var authObj = new Auth({
dateEffective: moment(Date.now,"YYYY MM DD").format("YYYY MM DD"),
username: '',
password: ''
})
callback(this.auth);
},
name: function(callback){
var nameObj = new Name({
title: '',
firstName: '',
middleName: '',
familyName: '',
suffix: ''
});
callback(this.name);
},
email: function(callback){
var emailObj = new Email({
emailAddress: '',
recoveryType: '',
commType: ''
})
callback(this.email);
}
}, function(err, result){
if (err) {return next(err);}
var user = new UserObj({
authData: [result.auth],
nameData: [result.name],
emailData: [result.email]
})
res.json(user);
})
};
注意:我尝试使用callback(result.auth.authObj)
,callback(result.authObj)
之类的语法,但这些都不起作用:
如果在此情况下任何人都可以提供async
的想法,请多谢!
答案 0 :(得分:0)
我需要使用“ callback(null,authObj)”,“ callback”来代替“ callback(this.auth)”,“ callback(this.name)”,“ callback(this.email)”等。 (null,nameObj)','callback(null,emailObj)'。