猫鼬预先验证架构的设置值

时间:2018-10-27 19:45:16

标签: node.js mongoose

好吧,经过一整个下午的尝试,但没有成功,我在这里尝试一下。 我定义了一个猫鼬模式:

// task.model.js
const task = new Schema({
    createdAt: { type: Date, required: true },
    priority: { type: Number, required: true, default: 0 },
    from: { type: Schema.Types.ObjectId, ref: 'Destination', required: true },
    to: { type: Schema.Types.ObjectId, ref: 'Destination', required: true }
})

{
    _id: 5bd4add06bb1b61aa41ab177,
    createdAt: 2018-10-27T18:26:15.759Z,
    from: 5bd379d9bc3b20174cf75ea0,
    to: 5bd379d9bc3b20174cf75ea7,
}
// from and to are set by the client, so I have _id there

// destination.model.js
const destination = new Schema({
    name: { type: String, required: true },
    priority: { type: Number, required: true }
})
{
    _id: 5bd379d9bc3b20174cf75ea0
    name:"G0",
    priority:5
}

现在在我的pre的{​​{1}}钩子中,我想基于对validate集合的查询来设置模式的优先级。

task

但是它似乎并没有设置`Destination的任务,它始终是未定义的,也许我遗漏了一些东西或者不得不等到查询完成后才知道。< / p>

编辑:另一个问题是,我应该将此逻辑放在客户端还是像在服务器端一样?

0 个答案:

没有答案
相关问题