[英]Mongoose error: Schema hasn't been registered for model when populate
I am trying to join two collections and being able to get the combined data.我正在尝试加入两个集合并能够获得合并的数据。 To do so using Mongoose, i am supposed to use the populate syntax to achieve that.要使用 Mongoose 做到这一点,我应该使用 populate 语法来实现这一点。 I am receiving error that the Schema Schema hasn't been registered for 'User_Fb'.我收到错误,提示尚未为“User_Fb”注册架构架构。 From my code, I have exported the models and required in my server.js but the error is still appearing.从我的代码中,我已经在我的 server.js 中导出了模型和所需的模型,但错误仍然出现。 What have I done wrong?我做错了什么?
feed_post.model.js feed_post.model.js
var mongoose = require('mongoose');
var conn_new_app = mongoose.createConnection('mongodb://localhost/new_app');
var User_fb = require('../models/fb_db.model');
var Schema = mongoose.Schema;
var feed_postSchema = new Schema({
user_id: { type: Schema.Types.ObjectId, ref: 'User_Fb' },
content: String,
location: String,
image: [{ type : String }]
});
var Feed_Post = conn_new_app.model('Feed_Post', feed_postSchema);
module.exports = Feed_Post;
fb_db.model.js fb_db.model.js
var mongoose = require('mongoose');
var conn_new_app = mongoose.createConnection('mongodb://localhost/new_app');
var Feed_Post = require('../models/feed_post.model');
var user_fb = new mongoose.Schema({
name: String,
location: String,
fb_id: Number
});
var User_Fb = conn_new_app.model('User_Fb', user_fb);
module.exports = User_Fb;
server.js服务器.js
var express = require('express'),
mongoose = require('mongoose'),
User = require('./app/models/user.model'),
Post = require('./app/models/post.model'),
Maptest = require('./app/models/maptest.model'),
Feed_Post = require('./app/models/feed_post.model'),
User_Fb = require('./app/models/fb_db.model'),
app = express();
app.get('/testget', function(req,res){
Feed_Post.findOne().populate('user_id').exec(function(err, c) {
if (err) { return console.log(err); }
console.log(c.fk_user.userName);
});
});
UPDATED from Pier-Luc Gendreau Answer's更新自 Pier-Luc Gendreau Answer's
fb_db.model.js fb_db.model.js
module.exports = function (connection) {
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user_fb = new mongoose.Schema({
name: String,
location: String,
fb_id: Number
});
return connection.model('User_FB', user_fb);;
}
feed_post.model.js feed_post.model.js
module.exports = function (connection) {
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var feed_postSchema = new Schema({
user_id: { type: Schema.Types.ObjectId, ref: 'User_Fb' },
content: String,
location: String,
image: [{ type : String }]
});
return connection.model('Feed_Post', feed_postSchema);;
}
server.js服务器.js
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
conn_new_app = mongoose.createConnection('mongodb://localhost/new_app'),
User_Fb = require('./app/models/fb_db.model')(conn_new_app),
Feed_Post = require('./app/models/feed_post.model')(conn_new_app);
app.get('/testget', function(req,res){
Feed_Post.find().populate('user_id').exec(function(err, res) {
if (err) { return console.log(err); }
console.log(res);
});
});
这就是我要做的全部Customer.findOne({}).populate({ path: 'created_by', model: User })
而不是这个Category.findOne({}).populate({'author'})
IF YOU (really) USE MULTIPLE mongoDB CONNECTIONS如果您(真的)使用多个 mongoDB 连接
I do realise that this is not the case for the OP, but if you genuinely use multiple connections you MUST provide the model when using .populate(), as mongoose will only "find" models on the same connection.我确实意识到这不是 OP 的情况,但是如果您真正使用多个连接,则必须在使用 .populate() 时提供模型,因为 mongoose 只会在同一连接上“找到”模型。 ie where:即在哪里:
var db1 = mongoose.createConnection('mongodb://localhost:27017/gh3639');
var db2 = mongoose.createConnection('mongodb://localhost:27017/gh3639_2');
var userSchema = mongoose.Schema({
"name": String,
"email": String
});
var customerSchema = mongoose.Schema({
"name" : { type: String },
"email" : [ String ],
"created_by" : { type: mongoose.Schema.Types.ObjectId, ref: 'users' },
});
var User = db1.model('users', userSchema);
var Customer = db2.model('customers', customerSchema);
Correct:正确的:
Customer.findOne({}).populate('created_by', 'name email', User)
or或者
Customer.findOne({}).populate({ path: 'created_by', model: User })
Incorrect (produces "schema hasn't been registered for model" error):不正确(产生“模式尚未为模型注册”错误):
Customer.findOne({}).populate('created_by');
The problem is that you are creating a new connection in each and every model, so you end up with a bunch of different connection objects.问题是您在每个模型中都创建了一个新连接,因此最终会得到一堆不同的连接对象。 Even though they are pointing to the same database, mongoose models don't know about other connections.即使它们指向同一个数据库,猫鼬模型也不知道其他连接。 You should instead create the connection object in your main app and then pass it around.您应该在主应用程序中创建连接对象,然后将其传递。
server.js服务器.js
var express = require('express');
var mongoose = require('mongoose');
var app = express();
var conn_new_app = mongoose.createConnection('mongodb://localhost/new_app');
var Feed_Post = require('./app/models/feed_post.model')(conn_new_app);
app.get('/testget', function(req,res){
Feed_Post.findOne().populate('user_id').exec(function(err, c) {
if (err) { return console.log(err); }
console.log(c.fk_user.userName);
});
});
Then modify your models so they are actually a function:然后修改您的模型,使它们实际上是一个函数:
feed_post.model.js feed_post.model.js
module.exports = function (connection) {
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var feed_postSchema = new Schema({
user_id: { type: Schema.Types.ObjectId, ref: 'User_Fb' },
content: String,
location: String,
image: [{ type : String }]
});
return connection.model('Feed_Post', feed_postSchema);;
}
Also, like Adrian mentions, you can use mongoose as a singleton.此外,就像 Adrian 提到的那样,您可以将 mongoose 用作单例。 However, I do recommend using createConnection
as you did because if you ever need a second connection, you won't have to refactor the connection code to your first database.但是,我建议您像以前一样使用createConnection
,因为如果您需要第二个连接,则不必将连接代码重构到您的第一个数据库。
It looks like you are creating a different database connection for each model, which isolates the models from each other.看起来您正在为每个模型创建不同的数据库连接,这将模型彼此隔离。 Mongoose must assume this isolation, because they could exist on different databases or even database servers. Mongoose 必须承担这种隔离,因为它们可能存在于不同的数据库甚至数据库服务器上。
Try connecting once, and just calling mongoose.model()
instead of connection.model()
when defining your models.尝试连接一次,并在定义模型时调用mongoose.model()
而不是connection.model()
。 Mongoose is a singleton by default. Mongoose 默认是单例的。
就我而言,这个问题是因为我没有将ref model
包含到应用程序中。
You can get the field you want by using select
您可以使用select
获取您想要的字段
Example to get the email of the customer:获取客户电子邮件的示例:
Customer.findOne({}).populate({ path: 'created_by', model: User, select: 'email' })
More Details in mongoose documentation猫鼬文档中的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.