问题描述
我有两个模式分别保存在models文件夹中的两个分别名为user.js
和comments.js
的文件中。
当用户通过注册页面进行注册时,我在这里使用的第一个User schema
会被保存在mongoDB中。之后,他可以通过登录页面使用这些数据进行登录。
我创建的第二个模式是针对Comments
的,我正在将其用于某些把手页面并存储用户注释。
因此,我想通过插值方式在车把页面中同时使用两种模式数据:对于用户模式:
{{user.name}}
,并且对于注释架构:{{comments.ClientId }}
这两个数据已经存储在数据库中。
但是在这里,我可以获取加载时在hbs页面上反映的用户数据,但是我无法在同一页面上看到评论数据。我不知道为什么?
P.S。 user & comments
是我在object
页面上创建的auth.js
,我在用户注册时在其中存储数据。
所以请让我知道如何直接与interpolation
一起使用它?
var userSchema = new mongoose.Schema({
email: {
type: String,unique: true,required: true
},name: {
type: String,tag: {
type: String
}
});
var commentSchema = new mongoose.Schema({
ClientId: {
type: String,unique: true
},AddtasksMain : [{
AddtasksId: String,comments: [
{
username: String,comment: String
}
]
}]
});
这是我用于注册的路线:
router.route('/register')
.get(function(req,res,next) {
res.render('register',{ title: 'Register a new account'});
})
.post(function(req,next) {
req.checkBody('name','Empty Name').notEmpty();
req.checkBody('email','Invalid Email').isEmail();
req.checkBody('password','Empty Password').notEmpty();
req.checkBody('password','Password do not match').equals(req.body.confirmPassword).notEmpty();
var errors = req.validationErrors();
if (errors) {
res.render('register',{
name: req.body.name,email: req.body.email,errorMessages: errors
});
} else {
var user = new User();
user.name = req.body.name;
user.email = req.body.email;
user.tag = "Client";
user.setPassword(req.body.password);
user.save(function (err,data) {
if (err) {
res.render('register',{errorMessages: err});
} else {
var comments = new Comments();
comments.ClientId = data._id;
comments.save(function (err,data) {
if (err) {
console.log(err);
} else {
console.log("success");
}
});
}
});
}
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)