Skip to content Skip to sidebar Skip to footer

Mongoose Is Not Populating (.populate()) On Production (heroku), But Works On Local

Essentially I am having one of those moments. My app's on Heroku, and the DB it uses is mLab (MongoDB). It works on local (Cloud9), but not on production (Heroku). I can't get .p

Solution 1:

Your API function looks ok.

I suspect your issue is with how your models are setup, or what is in your data-base. I had similar issues the first time I tried to use Heroku, because Localhost is more forgiving.

In order for your API to work, the following 3 things must be setup:

(1) Model file: people.js

must look like something like:

var mongoose = require("mongoose");
varSchema = mongoose.Schema;

var peopleSchema = newSchema({

  name: {
    type: String,
    required: true,
    trim: true
  },

  friends: [{
    type: Schema.Types.ObjectId,
    ref: "Friends"
  }]
});

constPeople = mongoose.model('Peoples', peopleSchema);

module.exports = People;

And then you must have a 'Friends' model, that 'People' is referencing.

(2) Model file: friends.js

must look something like:

var mongoose = require("mongoose");
varSchema = mongoose.Schema;

// Create the Comment schemavar friendsSchema = newSchema({

  friend_name: {
    type: String,
    required: true,
    trim: true
  },
});

constFriends = mongoose.model('Friends', friendsSchema);

module.exports = Friends;

And lastly, in order for .Populate to work, you need at least two docs in the database.

(3) Database must containa Person doc and a Friend doc 

must look something like:

people.js : 
    "_id": {
            "$oid": "5bef3480f202a8000984b3c5"
    }, 
    "name": "Monica Geller""friends": [
        {
            "$oid": "5bef3480f202a8000984b5b4"
        }
    ]

friends.js :
    "_id": {
            "$oid": "5bef3480f202a8000984b5b4"
    },
    "friend_name": "Rachel Green"

Hopefully this helps, or gets you closer to your answer.

Solution 2:

It was a version issue.

Had to ensure that all platforms (mLab, and my local database) were using the same version of Mongoose.

npm install mongoose@5.4.8--save 

Post a Comment for "Mongoose Is Not Populating (.populate()) On Production (heroku), But Works On Local"