使用下划线 js map 函数时,Mongo 引用字段未实例化

Mongo reference field not instantiating when using underscore js map function,

本文关键字:引用 Mongo 字段 实例化 下划线 js map 函数      更新时间:2023-09-26

网格组件模型具有组件模型的引用 ID(字段是组件 ID)

_.map(components,function (component){
        var foo = (new Component())._id;
        component.componentid = foo;
        var gc = new GridComponent(component);
        assert(typeof gc.componentid !== 'undefined');
    })

尚未失败

_.map(components,function (component){
        var foo = (new Component())._id;
        var testComponent = JSON.parse(JSON.stringify(component));
        testComponent['componentid'] = component._id;
        var gc = new GridComponent(testComponent);
        assert(typeof gc.componentid !== 'undefined');
    })

通过,谁能解释为什么

与猫鼬和mongodb一起使用的模型(最新版本)

var GridComponentSchema = new Schema({
   name: String,
   quantity: Number,
   componentid: {
      type: Schema.Types.ObjectId,
      ref: 'Component'
   },
   isNumeric: Boolean,
   maxlen: Number,
   processid: {
      type: Schema.Types.ObjectId,
      ref: 'lgProcess'
   }
})

每个猫鼬文档,

文档有一个toObject方法,它将猫鼬文档转换为普通的javascript对象。此方法接受一些选项。我们可以在此处声明选项,并将其应用于默认情况下的所有这些架构文档,而不是在每个文档的基础上应用这些选项。

或者另一种方法来JSON处理