猫鼬不在生产(Heroku)上填充(.populate()),但在本地 [英] Mongoose is not populating (.populate()) on Production (Heroku), but works on Local
问题描述
基本上,我正在其中一个时刻.我的应用程序在Heroku上,它使用的数据库是mLab(MongoDB).
Essentially I am having one of those moments. My app's on Heroku, and the DB it uses is mLab (MongoDB).
- 它在本地(Cloud9)上有效,但在生产环境(Heroku)上无效.
- 我无法获得 .populate()进行生产.
- It works on local (Cloud9), but not on production (Heroku).
- I can't get .populate() to work on production.
您是否在下面的代码段(代码段)中看到任何可能导致Heroku在本地运行的失败的空白?
Do you see any gaps in my code below (snippet) that may cause Heroku to fail, while it works on local?
谢谢.
(我曾尝试清除数据库(删除数据库并制作一个新数据库.另外,我在此站点上也遇到过类似的问题.我也曾尝试过' heroku local --tail ')命令在我的本地计算机上调试并运行它;它在本地计算机上运行...只是不在Heroku上;似乎有故障.)
(I have tried purging the DB (deleting the DB and making a new one. Also I've for similar questions on this site. Also I have tried 'heroku local --tail' command to debug and ran it on my local machine; it works on local... Just not on Heroku; appears buggy.)
People.find(id).populate("friends").exec(function(err, user){
if(err){
console.log("! Error retrieving user. " + err);
reject ("! Error retrieving user. " + err);
}
else {
console.log("0! Friends should be populated: " + user);
resolve(user);
}
});
我的模特:
var mongoose = require('mongoose');
var personSchema = mongoose.Schema({
name: String,
friends: [
{
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Person"
},
name: String
}
],
username: String,
password: String,
});
module.exports = mongoose.model("Person", personSchema);
推荐答案
您的API函数看起来还不错.
Your API function looks ok.
我怀疑您的问题与模型的设置方式或数据库中的内容有关.第一次尝试使用Heroku时,我遇到了类似的问题,因为Localhost更宽容.
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.
为了使您的API能够正常工作,必须设置以下三项内容:
In order for your API to work, the following 3 things must be setup:
(1) Model file: people.js
必须看起来像这样:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var peopleSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
friends: [{
type: Schema.Types.ObjectId,
ref: "Friends"
}]
});
const People = mongoose.model('Peoples', peopleSchema);
module.exports = People;
然后您必须具有人"引用的朋友"模型.
And then you must have a 'Friends' model, that 'People' is referencing.
(2) Model file: friends.js
看起来必须像这样:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Create the Comment schema
var friendsSchema = new Schema({
friend_name: {
type: String,
required: true,
trim: true
},
});
const Friends = mongoose.model('Friends', friendsSchema);
module.exports = Friends;
最后,为了使.Populate工作,数据库中至少需要两个文档.
And lastly, in order for .Populate to work, you need at least two docs in the database.
(3) Database must contain a Person doc and a Friend doc
看起来必须像这样:
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.
这篇关于猫鼬不在生产(Heroku)上填充(.populate()),但在本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!