【问题标题】:Is it possible to reference two schemas to eachother in Mongoose?是否可以在 Mongoose 中相互引用两个模式?
【发布时间】:2015-12-02 15:19:59
【问题描述】:

我有两个架构,我希望能够从另一个访问它们。我正在尝试做这样的事情:

//email.js

var mongoose = require('mongoose')
    ,Schema = mongoose.Schema
    , FoodItemSchema = require('../models/fooditem.js')
    , UserSchema = require('../models/user.js').schema
    , User = require('../models/user.js').model

    console.log(require('../models/user.js'));

    var emailSchema = new Schema({
        From : String,
        Subject : FoodItemSchema,
        Body : String,
        Date: Date,
        FoodItems : [FoodItemSchema],
        Owner : { type : Schema.Types.ObjectId , ref: "User" }
    });

    module.exports = {
        model: mongoose.model('Email', emailSchema),
        schema : emailSchema 
    }

//user.js

var mongoose = require('mongoose')
    ,Schema = mongoose.Schema
    , Email = require('../models/email.js').model
    , EmailSchema = require('../models/email.js').schema


console.log(require('../models/email.js'));

var userSchema = new Schema({
    googleID : String,
    accessToken : String,
    email : String,
    openId: Number,
    phoneNumber: String,
    SentEmails : [EmailSchema]
    // Logs : [{type: Schema.ObjectId, ref: 'events'}]
});
module.exports =  {
    model :  mongoose.model('User', userSchema),
    schema : userSchema
}

第一个 console.log() 打印空字符串,第二个按预期打印。我觉得我什至在创建变量之前就试图在其他模式中获取变量。有一个常见的解决方法吗?或者我应该在我的设计中避免双重依赖?

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    是的,您可以在 Mongoose 中创建交叉引用。但是没有办法在 Node.js 中创建循环依赖。不过,您不需要这样做,因为不需要用户模式来创建引用:

    var mongoose = require('mongoose')
      , Schema = mongoose.Schema
      , FoodItemSchema = require('../models/fooditem.js');
    
    var emailSchema = new Schema({
        From: String,
        Subject: FoodItemSchema,
        Body: String,
        Date: Date,
        FoodItems: [FoodItemSchema],
        Owner: { type: Schema.Types.ObjectId , ref: 'User' }
    });
    
    module.exports = {
        model: mongoose.model('Email', emailSchema),
        schema: emailSchema 
    }
    

    【讨论】:

      【解决方案2】:

      您可以定义 Schema Add 语句来描述公共属性:

      var mongoose = require('mongoose')
        , Schema = mongoose.Schema;
      
      module.exports = exports = function productCodePlugin(schema, options) {
        schema.add({productCode:{
          productCode : {type : String},
          description : {type : String},
          allowed : {type : Boolean}
        }});
      };
      

      然后将 add 语句要求到多个架构定义文件中。

      var mongoose = require('mongoose')
        , Schema = mongoose.Schema
        , ObjectId = Schema.ObjectId
        , productCodePlugin = require('./productCodePlugin');
      
      var ProductCodeSchema = new Schema({
      });
      ProductCodeSchema.plugin(productCodePlugin);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-29
        • 2017-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多