Error with schema, model and controller (mongoose)

0

I'm developing an app with node.js, mongodb, mongoose and I get an error when I added these files.

This is the "model.js" file:

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

var RequestDiffSchema = new Schema({
  id: {
    type: Number,
    unique: true,
    required:true
  },
  solicitudId: { 
    type: Number,
    required: true
  },
  errors: { 
    type: String,
    required:true
  }
}, { collection: 'requestdiff' });

module.exports = mongoose.model('RequestDiff', RequestDiffSchema);

And this is the "controller.js" file:

'use strict';

var mongoose = require('mongoose'),
    RequestDiff = mongoose.model('RequestDiff'),
    q = require('q'),
    Promise = q.Promise;


var RequestDiffController = {


  // Aquí van las funciones


};

module.exports = RequestDiffController;

I get an error when I raise the server and I can not fix it, did something wrong? some mistake? or am I missing a step?

The error that throws me by console is:

Error initializing middleware
MissingSchemaError: Schema hasn't been registered for model "RequestDiff".
Use mongoose.model(name, schema)
    at Mongoose.model (/Documentos/api/backend.server/node_modules/mongoose/lib/index.js:349:13)
    at Object.<anonymous> (Documentos/api/backend.server/api/controllers/requests-diff.js:7:28)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)

Is it because the model does not take me well? The routes are correct and I do not see the fault ...

    
asked by Norak 07.09.2017 в 17:49
source

1 answer

1

I just solved the error. After all, it was foolishness that gave me war, but just in case someone else happens ...

The problem was that the attribute defined in the Schema of the model.js as "errors" is a mongoose reserved word . I solved it by changing the name and it works perfectly!

    
answered by 08.09.2017 / 10:18
source