猫鼬模式参考返回未定义

问题描述

我只是想从 productSchema 中引用 accountSchema,但我收到了一个未定义的结果。我想我已经正确地引用了“帐户”,并且我正在正确地创建和保存产品。我的理解是 account 字段应该使用适当的 account objectId 填充自己,只是在模型中被引用。

//acount.model.js
const accountSchema = new Schema({
    email: { type: String,unique: true,required: true },passwordHash: { type: String,title: { type: String,firstName: { type: String,lastName: { type: String,acceptTerms: Boolean,role: { type: String,verificationToken: String,verified: Date,resetToken: {
        token: String,expires: Date
    },passwordReset: Date,created: { type: Date,default: Date.now },updated: Date
});
module.exports = mongoose.model('Account',accountSchema);
//product.model.js
const productSchema = new Schema({
    title: { type: String,trim: true,description: { type: String,category: { type: String,trim: true},subCategory: { type: String,trim: true },quantity: { type: Number,price: { type: Number,updated: Date,account: { type: Schema.Types.ObjectId,ref: 'Account' }
});

module.exports = mongoose.model('Product',productSchema);
//products.controller.js

//routes
router.post('/',authorize([Role.Admin,Role.Vendor]),createSchema,create);

function createSchema(req,res,next) {
    const schema = Joi.object({
        title: Joi.string().required(),description: Joi.string().required(),category: Joi.string(),subCategory: Joi.string(),quantity: Joi.number(),price: Joi.number()
    });
    validateRequest(req,next,schema);
}

function create(req,next) {
    productService.create(req.body)
        .then(product => res.json(product))
        .catch(next);
        
}
//product.service.js
async function create(params) {
    const product = new db.Product(params);
    // save account
    await product.save();
    // res.json(product);
    console.log(basicDetails(product));
    return basicDetails(product);
    
}

function basicDetails(product) {
    const { id,title,description,category,subCategory,quantity,price,account } = product;
    return { id,account };
}
//console.log output of basicDetails(product)
{
  id: '600614014d927f29a0fbb162',title: 'delete from mcjaggerrrrrrrr6789',description: 'testing delete from role.vendor mcjaggerrrrrrrrrr',category: undefined,subCategory: undefined,quantity: undefined,price: undefined,account: undefined
}

category、subCat、quant 和 price 都未定义,因为我没有为它们输入值,但 account 未定义,我不知道为什么。任何帮助表示赞赏!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...