最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论