简体   繁体   中英

MongoDB + Node.js : How to use a Schema from an external file for another Schema?

I have a classes (or models) that needs to use another class as part of its properties as shown below.

** Header for both files **

var mongoose = require('mongoose'),
Schema =       mongoose.Schema;

item.js

module.exports = function() {
    var ItemSchema = new Schema({
        name: String,
        cost: Number
    });
    mongoose.model('Item', ItemSchema);
}

receipt.js

ItemModel = require('./item.js');

var Item = mongoose.model('Item');

module.exports = function() {

    var LineItemSchema = new Schema({
        item:   Item,
        amount: Number
    });

    var LineItem = mongoose.model('LineItem', LineItemSchema);

    var ReceiptSchema = new Schema({
        name:   String,
        items:  [LineItemSchema]
    });
    mongoose.model('Receipt', ReceiptSchema);
}

In the LineItem class, I'm trying to set the type of the variable 'item' to the class type, Item, node.js or mongoose.js is screaming at me about it saying that there's a type error.

How can I use a Schema "type" from an external file?

I have no idea why you are wrapping all of this in an anonymous function. But to reference a schema from another schema, you can do the following:

var LineItemSchema = new Schema({
    item: {
        type: Schema.ObjectId,
        ref: 'Item'
    },
    amount: Number
});

And of course you need to require the Schema object:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

In item.js have it return the schema from a self-executing function.

module.exports = (function() {
    var ItemSchema = new Schema({
        name: String,
        cost: Number
    });
    mongoose.model('Item', ItemSchema);
    return ItemSchema;
})();

Then in receipt.js you now can use the schema just like you used LineItemSchema.

var ItemSchema = require('./item.js');

// This should still create the model just fine.
var Item = mongoose.model('Item');

module.exports = function() {

var LineItemSchema = new Schema({
    item:   [ItemSchema], // This line now can use the exported schema.
    amount: Number
});

var LineItem = mongoose.model('LineItem', LineItemSchema);

var ReceiptSchema = new Schema({
    name:   String,
    items:  [LineItemSchema]
});
mongoose.model('Receipt', ReceiptSchema);

}

This is all speculation and untested.

I was also facing the same issue then found that I can fix it by just exporting Schema from Item.js

Example:

Item.js
-----------

    const ItemSchema = new Schema({
        name: String,
        cost: Number
    });
    module.exports = ItemSchema;

and can use this schema in other models like this

    const ItemSchema = require('./item.js');

    const LineItemSchema = new Schema({
        item: {
            type: ItemSchema,
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM