【问题标题】:How do I save after push to referenced model?推送到引用模型后如何保存?
【发布时间】: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


【解决方案1】:

Mongodb 本质上是异步的。您需要在 domain.save 的回调下移动 client.save()。 For more help

Client.findOne({_id: req.params.client_id},function(err,client) {

    var domain = new Domain({
        _client: client.id,
        name: req.body.name,
        startDate: req.body.startDate,
        endDate: req.body.endDate,
        realPrice: req.body.realPrice,
        billedPrice: req.body.billedPrice
    });

    domain.save(function(err, result) {
        client.domains.push(result._id);
        client.save();
    });

    res.redirect("/"); 
});

编辑

我已经快速尝试了您的查询,它对我来说很好。

检查这个Sample Code Snippet

【讨论】:

  • 这就是我最初尝试的方法,但 mongo(ose) 让我遇到了这个错误:{ [MongoError: Field name duplication not allowed with modifiers] name: 'MongoError', err: 'Field name duplication not allowed with modifiers', code: 10150, n: 0, connectionId: 333, ok: 1 }
  • 请再次检查我的代码 - 我使用了 cli,但它应该是客户端。
  • 我已经注意到它并在尝试之前进行了更改。
  • 我试过你的查询。对我来说工作得很好。检查我添加到答案中的链接
  • 在每一步都添加控制台,我相信你会遇到问题。我注意到的另一件事,不认为它会造成太大问题,但仍然检查它 - 你已经使用了这个“_client:client.id”,但你的域架构没有“_client”字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
相关资源
最近更新 更多