在Mongoose中居住不起作用

时间:2017-12-30 17:57:48

标签: node.js mongodb mongoose mongoose-schema mongoose-populate

我试图通过引用方法来执行关联。有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: "madhur@google.com",
  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: "madhur@google.com"}).
populate("products").
exec(function(err,user){
    if(err){
        console.log(err);
    }
    else {
        console.log(user);
    }
});

代码输出

用户创建(成功)

[{
  products: [],
  _id: 5a47acb0317d4e3c2081b8ce,
  email: 'madhur@google.com',
  name: 'Maddy',
  __v: 0
}]

产品创建和关联(成功)

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

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

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

有谁能请解释我如何正确地做到这一点? 在此先感谢

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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: 'madhur@google.com',
    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: 'madhur@google.com'})
    .populate('products')
    .exec(function(err, user) {
        if (err) {
            console.log(err);
        } else {
            console.log(user);
        }
    });

答案 2 :(得分:0)

解决 做了以下

  1. 使用以下命令npm remove mongoose然后npm install mongoose@4.10.8 --save

  2. 将我的Mongoose版本从5.00x降级到4.10.8
  3. 在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);
    
  4. 感谢Stack社区的尝试!