在Mongoose中居住不起作用

问题描述 投票:0回答:3

我试图通过引用方法执行关联。有2种型号:1。用户2.产品

我已经建立了一对一的关系,1个用户可以拥有多个产品。用户创建成功产品创建成功

代码输入

var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/product_demo_x9");

产品时间表

var productSchema = new mongoose.Schema({
  category : String,
  Brand: String
});
var Product = mongoose.model("product", productSchema);

用户架构

var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
  {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Product"
  }
]
});`
var User = mongoose.model("user", userSchema);

用户创建

User.create({
  email: "[email protected]",
  name: "Maddy"
},function(err,newUser){
    if(err){
       console.log(err);
    }
    else {
       console.log(newUser);
    }
});

产品创造

Product.create({
    category: "Smartwatches",
    Brand: "Samsung and Google"
}, 
function(err,product){
    console.log(product);
    User.findOne({name : "Maddy"},function(err,foundUser){
        if(err) {
            console.log(err);
        }
        else {
            foundUser.products.push(product);
            foundUser.save(function(err,updatedUser){
                if(err){
                     console.log(err);
                }
                else {
                        console.log(updatedUser);
                    }
                });
            }
    });
});

在控制台上显示关联数据

User.find({email: "[email protected]"}).
populate("products").
exec(function(err,user){
    if(err){
        console.log(err);
    }
    else {
        console.log(user);
    }
});

代码输出

用户创建(成功)

[{
  products: [],
  _id: 5a47acb0317d4e3c2081b8ce,
  email: '[email protected]',
  name: 'Maddy',
  __v: 0
}]

产品创建和关联(成功)

{
  _id: 5a47acd53c771123b4018ff1,
  category: 'Smartwatches_2',
  Brand: 'Samsung and Google',
  __v: 0
}
{
  products: [ 5a47acd53c771123b4018ff1 ],
  _id: 5a47acb0317d4e3c2081b8ce,
  email: '[email protected]',
  name: 'Maddy',
  __v: 1
}

使用填充显示嵌入数据 - 失败!!

{ MissingSchemaError: Schema hasn't been registered for model "products".
Use mongoose.model(name, schema)
    at new MissingSchemaError

任何人都可以解释我如何正确地做到这一点?提前致谢

node.js mongodb mongoose mongoose-schema mongoose-populate
3个回答
1
投票

型号名称区分大小写

'Product'不等于'product',当你创建一个模型作为'product'(单数)时,它将它转换为复数,即'products',这是默认的猫鼬行为,可以被覆盖。

所以改变以下内容:

var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
  {
    type: mongoose.Schema.Types.ObjectId,
    ref: "products" //<---- changed 'Product' to 'products'
  }
]
});`
var User = mongoose.model("user", userSchema);

0
投票

试试这个

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/product_demo_x9');

var productSchema = new mongoose.Schema({
    category: String,
    Brand: String
});
var Product = mongoose.model('Product', productSchema);

var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Product'
    }
]
});
var User = mongoose.model('User', userSchema);

User.create({
    email: '[email protected]',
    name: 'Maddy'
}, function(err, newUser) {
    if (err) {
        console.log(err);
    } else {
        console.log(newUser);
    }
});

Product.create({
        category: 'Smartwatches',
        Brand: 'Samsung and Google'
    },
    function(err, product) {
        console.log(product);
        User.findOne({name: 'Maddy'}, function(err, foundUser) {
            if (err) {
                console.log(err);
            } else {
                foundUser.products.push(product);
                foundUser.save(function(err, updatedUser) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(updatedUser);
                    }
                });
            }
        });
    });

User.find({email: '[email protected]'})
    .populate('products')
    .exec(function(err, user) {
        if (err) {
            console.log(err);
        } else {
            console.log(user);
        }
    });

0
投票

解决了以下问题

  1. 使用以下命令npm remove mongoose然后npm install [email protected] --save 将我的Mongoose版本从5.00x降级到4.10.8
  2. 在app.js文件中进行了以下更改 var userSchema = new mongoose.Schema({ email: String, name: String, products: [ { type: mongoose.Schema.Types.ObjectId, ref: "product" //<---- changed 'Product' to 'product' } ] });` var User = mongoose.model("user", userSchema);

感谢Stack社区试用!

© www.soinside.com 2019 - 2024. All rights reserved.