Home > Enterprise >  Should dependency schema be compiled too?
Should dependency schema be compiled too?

Time:01-01

In mongoose I have the following Schemas in a single file:

var CoinSchema = new Schema({
  ...
});
var WalletSchema = new Schema({
  coins: {
    type: [CoinSchema]
  }
});

I don't know how should I export this Schema.
Do I need to export the model of both of these ?
Or is exporting the dependent schema the way below, enough?

module.exports = mongoose.model('Tasks', TaskSchema);

CodePudding user response:

You should declare and export both models.
Also, you should change your coins property to be of type ObjectID, referencing the Coin model:

var CoinSchema = new Schema({
  ...
});

const coinModel = mongoose.model('Coin', CoinSchema);

var WalletSchema = new Schema({
  coins: {
      [
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Coin'
      ],
  }
});

const walletModel = mongoose.model('Wallet', WalletSchema);

module.exports = {
    coinModel,
    walletModel
}
  • Related