Mongoose 猫鼬不能让它工作

Mongoose 猫鼬不能让它工作,mongoose,Mongoose,非常简单的填充过程我只是缺少一些简单的东西。我不知所措。使用NodeJS,Mongoose进行简单的反应 用户模型 const Schema = mongoose.Schema; const UserSchema = new Schema({ email: { type: String, }, password: { type: String, }, books: [ { type: Schema.Types.ObjectId,

非常简单的填充过程我只是缺少一些简单的东西。我不知所措。使用NodeJS,Mongoose进行简单的反应

用户模型

const Schema = mongoose.Schema;
const UserSchema = new Schema({
  email: {
    type: String,
  },
  password: {
    type: String,
  },
  books: [
    {
      type: Schema.Types.ObjectId,
      ref: "Books"
    }
  ]
});

module.exports.User = mongoose.model("User", UserSchema);
图书模型

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const BooksSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "User"
  },
  title: {
    type: String
  },
  author: {
    type: String
  },
  description: {
    type: String
  }
});

module.exports.Books = mongoose.model("Books", BooksSchema);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const BooksSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "User"
  },
  title: {
    type: String
  },
  author: {
    type: String
  },
  description: {
    type: String
  }
});

module.exports.Books = mongoose.model("Books", BooksSchema);

Function call

router.get("/test", (req, res) => {
  User.find()
    .populate({path:"books",model:"Books"})
    .exec()
    .then(user => {
      res.json(user);
    });
});
函数调用

router.get("/test", (req, res) => {
  User.find()
    .populate("books")
    .exec()
    .then(user => {
      res.json(user);
    });
});
与我实际所做的相比,这是一个简短的概念。我以为我明白了,但显然不明白


目前,我有一个用户(A)有两本书。我相信当我在《邮递员》中运行这条路线时,我应该获得电子邮件、密码、id和一系列图书id……或者我认为是这样。请让我知道我做错了什么,或者给我一个简单的解释……谢谢……

您应该只导出模式:

module.exports = mongoose.model("User", UserSchema);
module.exports = mongoose.model("Book", BookSchema); // Notice here "Book"
然后,在执行查找时,请确保使用架构的确切名称:

router.get("/test", (req, res) => {
  User.find()
    .populate("books")
    .exec()
    .then(user => {
      res.json(user);
    });
});
我创建了您的模型/模式并对其进行了测试。如果您有任何问题,请告诉我

const Schema = mongoose.Schema;
const UserSchema = new Schema({
  email: {
    type: String,
  },
  password: {
    type: String,
  },
  books: [
    {
      type: [Schema.Types.ObjectId],//you have to make this array of ids 
      ref: "Books"
    }
  ]
});

module.exports.User = mongoose.model("User", UserSchema);
图书模型

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const BooksSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "User"
  },
  title: {
    type: String
  },
  author: {
    type: String
  },
  description: {
    type: String
  }
});

module.exports.Books = mongoose.model("Books", BooksSchema);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const BooksSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "User"
  },
  title: {
    type: String
  },
  author: {
    type: String
  },
  description: {
    type: String
  }
});

module.exports.Books = mongoose.model("Books", BooksSchema);

Function call

router.get("/test", (req, res) => {
  User.find()
    .populate({path:"books",model:"Books"})
    .exec()
    .then(user => {
      res.json(user);
    });
});

不适合我。假设用户的模式具有验证功能,那么在这场失败之外,该功能将完美无缺地工作。我相信“.populate('Book')”实际上应该是“.populate('books')”,因为这是我试图填充的字段。我想尽一切办法都试过了。你可能试过了,因为我在本地测试了我上面写的内容,它确实有效。在你的例子中,
书籍
正确,我的模式名称略有不同谢谢。我不得不改用actionCreator。我最终会回去使用。。。