javascript - Is it possible to reference two schemas to eachother in Mongoose? - Stack Overflow

I have two Schemas, and I want to be able to access both of them from the other one.. I am trying to do

I have two Schemas, and I want to be able to access both of them from the other one.. I am trying to do something like this:

//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
}

The first console.log() prints empty string and the second one prints as expected. I feel like I am trying to get the variables in the other schema even before they were created. Is there a mon workaround for this? Or should I avoid double dependencies in my design?

I have two Schemas, and I want to be able to access both of them from the other one.. I am trying to do something like this:

//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
}

The first console.log() prints empty string and the second one prints as expected. I feel like I am trying to get the variables in the other schema even before they were created. Is there a mon workaround for this? Or should I avoid double dependencies in my design?

Share Improve this question edited Oct 1, 2013 at 23:42 s_curry_s asked Oct 1, 2013 at 23:24 s_curry_ss_curry_s 3,4329 gold badges34 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Yes, you can create cross-references in Mongoose. But there is no way to create cyclic dependencies in Node.js. Though, you don't need to, because there is no need to require user schema in order to create a reference:

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 
}

You can define Schema Add statements to describe mon attributes:

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}
  }});
};

then require the add statement into multiple schema definition files.

var mongoose = require('mongoose')
  , Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId
  , productCodePlugin = require('./productCodePlugin');

var ProductCodeSchema = new Schema({
});
ProductCodeSchema.plugin(productCodePlugin);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745183411a4615523.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信