【发布时间】:2014-08-12 16:58:49
【问题描述】:
在向模型推送数据后尝试保存模型时遇到问题: 以下是架构:
var ClientSchema = new Schema({
email: {type: String, required: '{PATH} is required!', unique: 'This email already exists!'},
name: String,
nick: String,
domains: [{ type: Schema.Types.ObjectId, ref: 'Domain' }],
hosting: [{ type: Schema.Types.ObjectId, ref: 'Hosting'}]
});
var DomainSchema = new Schema({
name: {type: String, required: '{PATH} is required'},
startDate: {type: Date, required: '{PATH} is required'},
endDate: {type: Date, required: '{PATH} is required'},
realPrice: {type: Number, required: '{PATH} is required'},
billedPrice: {type: Number, required: '{PATH} is required'}
});
这是控制器的一部分:
...
Client.findOne({_id: req.params.client_id},function(err,client) {
var domain = new Domain({
name: req.body.name,
startDate: req.body.startDate,
endDate: req.body.endDate,
realPrice: req.body.realPrice,
billedPrice: req.body.billedPrice
});
domain.save(function(err) {
console.log(domain._id);
});
cli.domains.push(domain._id);
client.save(function(err,cli) {
// That's the one that makes it possible to save
// client.save();
});
res.redirect("/");
});
...
现在,
如果我将其保留为一个client.save(..),则域已保存,已推送但未保存客户端。
如果我取消注释另一个client.save(),一切都会保存下来(我猜)。
那么问题又来了,为什么我需要保存两次?
我在这里想念什么真的很简单吗?
不要误会我的意思——它有效,但我只需要理解它;)
提前感谢您的帮助。
更新/解决方案:
我遇到的所有问题都是因为我的 ubuntu 机器上安装的 mongodb 版本较旧(2.4.9),并且与 mongodb 使用的文档的版本控制存在一些冲突。我在另一台机器上检查了相同的代码,一切正常。这就是让我重新检查并安装更新的 mongodb 以及清理实际数据库的原因,这使得一切都按原样工作,当然 - 工作;)
【问题讨论】:
-
+1,用于更新/解决方案
标签: mongoose