0
votes

This is the model: /doc/proModel.js

module.exports = function (mongooseModels) {
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/DocTest');
var Schema = mongoose.Schema;

var ProSchema = new Schema({
    _id: {type: Schema.Types.ObjectId},
    Titre: {
        type: String,
        //required: true
    },
    Prenom: {
        type: String,
        //required: true
    },
    Nom: {
        type: String,
        //required: true
    }
});

var proModel = {
    Pro: mongoose.model("Pro", ProSchema)
};

return proModel;

This is the main.js:

var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/DocTest');
var proModel = require('./doc/proModel');

var pro1 = new proModel().Pro({
_id :{type: new mongoose.Types.ObjectId()},
Nom: "Ok"
});

pro1.save(function (err) {
if (err) return handleError(err);

});

This is the error I'm getting:

Exception has occurred: Error ReferenceError: handleError is not defined at /Users/maxime/Documents/Projets/Test/main.js:11:21 at Model.$wrapCallback (/Users/maxime/Documents/Projets/Test/node_modules/mongoose/lib/model.js:3835:16) at /Users/maxime/Documents/Projets/Test/node_modules/mongoose/lib/services/model/applyHooks.js:167:17 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9) at Timeout.Module.runMain [as _onTimeout] (module.js:606:11) at ontimeout (timers.js:386:14) at tryOnTimeout (timers.js:250:5) at Timer.listOnTimeout (timers.js:214:5)

Any idea would be greatly appreciated

Cheers,

1

1 Answers

0
votes

So there are multiple errors

first one handleError(err), your function handleErrordoes not exist.

second, the way you are saving your model is wrong. Here is how I would write your code :

/doc/proModel.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ProSchema = new Schema({
    _id: {type: Schema.Types.ObjectId},
    Titre: {
        type: String,
        //required: true
    },
    Prenom: {
        type: String,
        //required: true
    },
    Nom: {
        type: String,
        //required: true
    }
});

module.exports = mongoose.model("Pro", ProSchema);

/main.js

const mongoose = require('mongoose');
const ProModel = require('./doc/proModel');

mongoose.connect('mongodb://127.0.0.1/DocTest');

const pro1 = new ProModel({
    _id : new mongoose.Types.ObjectId(),
    Nom: "Ok"
});

const handleError = function() {
    console.error(err);
    // handle your error
};

pro1.save(function (err) {
    if (err) return handleError(err);
});

As per model organisation this is an app structure that I like

/models
  pro-model.js
  users-model.js
  books-model.js
  orders-model.js
  index.js

// /models/index.js file
const fs = require('fs');

fs.readdirSync(__dirname)
.forEach((file) => {
    if (file.endsWith("-model.js")) {
        const M = require(`./${file}`);
        exports[M.modelName] = M;
    }
});

then if you want to get all your models you can do :

const models = require('./models');

new models.Pro({
    _id : new mongoose.Types.ObjectId(),
    Nom: "Ok"
})
.save();