Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate an array of Model objects with mongoose

This is the schema that I use. As you can see survey_codes model path consists of an Array of ObjectIds.

...
var Schema = mongoose.Schema;
var Email = mongoose.SchemaTypes.Email;
var ObjectId = mongoose.SchemaTypes.ObjectId;

var RestaurantSchema = new Schema({
  id                    : {type: String, required: true, unique: true},
  name                  : {type: String, required: true},
  owner_name            : String,
  reservation_email     : Email,
  survey_url            : String,
  survey_codes          : [{type: ObjectId, ref: SurveyCode}],
  created_at            : {type: Date, default: Date.now}
});

var SurveyCodeSchema = new Schema({
  code                  : {type: String, unique: true, required: true},
  valid                 : {type: Boolean, default: true},
  create_date           : {type: Date, default: Date.now},
  used_date             : {type: Date, default: null}
});

And here the function i'm trying to use:

Restaurant
    .findOne({ id: self.param('id') })
    .populate('survey_codes')
    .exec(function(err, restaurant) {
      if (err)
        console.log('Error in view survey codes function');
      if (!restaurant || restaurant.survey_codes.length < 1)
        self.res.send('No survey codes are yet generated.');
      else
        self.res.send(restaurant.survey_codes);
    });

When I'm executing the function, it gives me this error:

Locomotive 0.3.7 application starting in development on http://0.0.0.0:3000

/home/pblondin/nodejs-dev/rezerve-locomotive/node_modules/mongoose/lib/utils.js:419
        throw err;
              ^
MissingSchemaError: Schema hasn't been registered for model "function model(doc, fields, skipId) {
    if (!(this instanceof model))
      return new model(doc, fields, skipId);
    Model.call(this, doc, fields, skipId);
  }".

I just can't get my head over this one. This is the first time that I post here, and I've noticed couple of you guys answer to similar questions, but the solution is not working in my case.

Thanks!

EDITED:

Here is some additional info:

1) A sample from Restaurants collection:

[
    {
        "__v": 1,
        "_id": "52617861b9ee6c171b000001",
        "id": "AAA",
        "name": "Name",
        "owner_name": "Owner",
        "reservation_email": "[email protected]",
        "survey_url": "[email protected]",
        "created_at": "2013-10-18T18:05:21.447Z",
        "survey_codes": [
            "52617864b9ee6c171b000002",
            "52617864b9ee6c171b000003",
            "52617864b9ee6c171b000004",
            "52617864b9ee6c171b000005",
            "52617864b9ee6c171b000006",
            "52617864b9ee6c171b000007",
            "52617864b9ee6c171b000008",
            "52617864b9ee6c171b000009",
            "52617864b9ee6c171b00000a",
            "52617864b9ee6c171b00000b"
        ]
    }
]

2) Version of dependencies:

mongoose: 3.6.20
mongodb: 1.3.19
locomotive: 0.3.7
locomotive-mongoose: 0.1.0
like image 997
pblondin Avatar asked Oct 18 '13 18:10

pblondin


People also ask

How do you populate an array?

In JavaScript, you can use the Array. fill() method to populate an array with a zero or any other value like an object or a string. This method replaces all elements in an array with the value you want to populate the array with and returns the modified array.

How does populate work Mongoose?

Mongoose Populate() Method. In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the other collection.

What is refPath Mongoose?

The refPath option is a more sophisticated alternative to ref . If ref is a string, Mongoose will always query the same model to find the populated subdocs. With refPath , you can configure what model Mongoose uses for each document. const book = await Product.

What is ObjectId in Mongoose?

ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose. ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.


1 Answers

SOLVED(!)

A simple typo in my model:

survey_codes          : [{type: ObjectId, ref: SurveyCode}],

ref as to be a model name, so 'SurveyCode' instead!

like image 53
pblondin Avatar answered Nov 15 '22 11:11

pblondin