Mongoose在Node JS中填充(Mongoose Populate in Node JS)

编程入门 行业动态 更新时间:2024-10-24 18:18:03
Mongoose在Node JS中填充(Mongoose Populate in Node JS)

我一直在努力关注Mongoose Population中的信息,但我得到了例外:

MissingSchemaError:尚未为模型“undefined”注册Schema。

我的代码是这样的:

mongoose = require('mongoose'); Schema = mongoose.Schema; mongoose.connect(MONGO_SERVER); ObjectId = Schema.ObjectId; var FirstSchema = new Schema({ label : String }); var SecondSchema = new Schema({ first_id : [{ type: mongoose.Schema.ObjectId, ref: 'First' }], type : String, ... }); var first= mongoose.model('First', FirstSchema); var second= mongoose.model('Second', SecondSchema); function test() { ... second.find({}).populate('first_id').exec(function(err,data){return true;}); ... }

并且错误发生在填充,我已经多次调整它在论坛上发现的不同答案,我相信它会是一些简单的东西,但是有人能指出我正确的方向吗?

干杯。

I've been trying to follow the information in Mongoose Population, but I'm getting the exception:

MissingSchemaError: Schema hasn't been registered for model "undefined".

The code I have goes like this:

mongoose = require('mongoose'); Schema = mongoose.Schema; mongoose.connect(MONGO_SERVER); ObjectId = Schema.ObjectId; var FirstSchema = new Schema({ label : String }); var SecondSchema = new Schema({ first_id : [{ type: mongoose.Schema.ObjectId, ref: 'First' }], type : String, ... }); var first= mongoose.model('First', FirstSchema); var second= mongoose.model('Second', SecondSchema); function test() { ... second.find({}).populate('first_id').exec(function(err,data){return true;}); ... }

And the error occurs on the populate, I've tweaked it a number of times to different answers found on forums, and I'm sure it will be something simple, but can someone point me in the right direction?

Cheers.

最满意答案

在您的模式定义中,我看到您已将“first_id”定义为第二个模式中的数组。 与关系数据库相比,这将是一对多关系,其中父表是第二个集合,第一个集合作为子集。 然后你做错了尝试用第一个填充第二个。

假设我有一个Users集合和一个Clients集合,其中每个客户端都有一个与之相关的用户。 然后代码将是:

var mongoose = require('mongoose');
mongoose.connect('mongodb://userName:password@server:port/dbname');
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function callback () {
    console.log('connected ');
});

var user = mongoose.Schema({
    userName: String
});

var client = mongoose.Schema({
    fk_user: { type: mongoose.Schema.ObjectId, ref: 'Users' },
    name: String
});

var UserModel = mongoose.model('Users', user);
var ClientModel = mongoose.model('Clients', client);

ClientModel.findOne().populate('fk_user').exec(function(err, c) {
    if (err) { return console.log(err); }

    console.log(c.fk_user.userName);
});
 

希望这能给你一些帮助。

In your schema definitions, I see that you have defined 'first_id' as an array in Second schema. Compared to a relational database, this will be like an one-to-many relationship in which the parent table is the Second collection, and First collection as the child. Then you're doing wrong trying to populate the second with the first.

Suppose I have a Users collection, and a Clients collection, in which each client has an user related to it. Then the code will be:

var mongoose = require('mongoose');
mongoose.connect('mongodb://userName:password@server:port/dbname');
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function callback () {
    console.log('connected ');
});

var user = mongoose.Schema({
    userName: String
});

var client = mongoose.Schema({
    fk_user: { type: mongoose.Schema.ObjectId, ref: 'Users' },
    name: String
});

var UserModel = mongoose.model('Users', user);
var ClientModel = mongoose.model('Clients', client);

ClientModel.findOne().populate('fk_user').exec(function(err, c) {
    if (err) { return console.log(err); }

    console.log(c.fk_user.userName);
});
 

Hope this give you some point to help.

更多推荐

本文发布于:2023-07-05 07:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Node   Mongoose   Populate   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!