Upgrade liana v7 mongoose

Hi, I try to upgrade my “forest-express-mongoose”: “6.7.2”, to “forest-express-mongoose”: “7.9.4”,
I followed this document Upgrade to v7 - Developer guide

But problem is I am getting some errors for some models like that

Cannot find the reference “Prices” on the model “Offers”.

because I was using my model just like below

const OffersSchema = new mongoose.Schema({
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
  },
  isActive: {
    type: Boolean,
    default: true,
  },
  name: {
    type: String,
    required: true,
  },
  meal: {
    // meal type
    type: Schema.Types.ObjectId,
    ref: 'MealCategories',
  },
  prices: [{ type: Schema.Types.ObjectId, ref: 'Prices' }],
});

module.exports = mongoose.model('Offers', OffersSchema);

and I changed to

module.exports = (mongoose, Mongoose) => {
const OffersSchema = Mongoose.Schema({
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
  },
  isActive: {
    type: Boolean,
    default: true,
  },
  name: {
    type: String,
    required: true,
  },

  schoolReference: {
    // school internal reference, ex: GE10
    type: String
  },
  room: {
    // room type
    type: Schema.Types.ObjectId,
    ref: 'RoomCategories',
  },
  meal: {
    // meal type
    type: Schema.Types.ObjectId,
    ref: 'MealCategories',
  },
  prices: [{ type: Schema.Types.ObjectId, ref: 'Prices' }],
});

return mongoose.model('Offers', OffersSchema);

}

like that so I am using some references for other models for that reason I think it is related but I don’t know solution so could u help me ?

Hey @codere :wave:

Did you also update your index.js loading all your models as well ?

Hi @jeffladiray yes I did.

const mongoose = require('mongoose');
const requireAll = require('require-all');
const fs = require('fs');
const path = require('path');
const config = require('../config/config');

const databasesConfiguration = require('../config/databases');

const models = {};
const connections = {};

const mongoUri = config.mongo.host;

mongoose.connect(mongoUri, {
  dbName: config.mongo.dbName,
  useCreateIndex: true,
  useNewUrlParser: true,
  poolSize: 2,
  useUnifiedTopology: true,
  useFindAndModify: false,
});


databasesConfiguration.forEach((databaseInfo) => {
  const connection = mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  console.log(modelsDir);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = require(path.join(modelsDir, file))(connection, mongoose);
        console.log(model);
        models[model.modelName] = model;
        console.log(model);
      } catch (error) {
        console.error(`Model creation error: ${error}`);
      }
    });
});



models.objectMapping = mongoose;
models.connections = connections;

module.exports = models;

and this one databases.js

const path = require('path');

const databaseOptions = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
};
module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models'),
  connection: {
    url: "mongodb://localhost:27017/db",
    options: { ...databaseOptions },
  },
}];

Could you also share the Prices models as well ?

Thanks in advance :pray:

1 Like
const mongoose = require('mongoose');
const { Schema } = mongoose;

module.exports = (mongoose, Mongoose) => {
  const PricesSchema = Mongoose.Schema({
    createdAt: {
      type: Date,
      default: Date.now
    },
    updatedAt: {
      type: Date,
    },
    season: {
      type: Schema.Types.ObjectId,
      ref: 'Seasons',
      require: true,
    },
    perNight: {
      type: Number,
    },
  });
 return mongoose.model('Prices', PricesSchema);
}

Well, I can’t spot anything obvious here. Can you finally share your project name so I can check your configuration on my end ?

Hello,
Did you try to replace Schema.Types.ObjectId by Mongoose.Schema.Types.ObjectId?

yes this way it was working but now I have to add everywhere mongoose right now

module.exports = (mongoose, Mongoose) => {

Do I need to use this ?

Yes, according the doc, you must prefix all your using Schema.types by the Mongoose object provided by the lambda function. :face_with_head_bandage:

why we are using same mongoose two time, I don’t get it ?

because I can’t override my models also

module.exports = (mongoose) => {
  const schema = mongoose.Schema({
    'country': String,
    // ...
  });

  return mongoose.model('companies', schema, );
};

for example why we are not using this use ?

moogoose is the instance connection and Mongoose, is the static reference of the Mongoose package. This is useful when our customers use several mongoose databases.
Why you don’t want to use Mongoose?

basically I am using mongodb database and without mongoose parameters v7 doesn’t work right ?
and are u sure about that we need two argument like that ?

According the documentation, the lambda function provides two arguments.

yes but I don’t understand why because it doesn’t makes sense for me.

also I will update my database structure after this upgrade should I backup database because I have some data in forest admin and I don’t want to lose them.

Hello,
Forest Admin will never remove your data.
This migration will not update your database structure. This migration is useful to make in compatible with our mongoose agent.
For your migration, the best practice is to run it on your staging environment before to run it on your production. If you you don’t have staging environment, you can run the migration on your machine with some data on your database and checks if all is ok.