node.js - 集合之间的 .populate() 连接没有出现
问题描述
有点挣扎从猫鼬填充。一直在查看各种教程和文档,但仍然不明白问题所在。我有 2 个模型:项目和类别。
Item.js 模型
const mongoose = require("mongoose");
const ItemSchema = new mongoose.Schema({
child: {
type: mongoose.Schema.Types.ObjectId,
ref: "category",
},
name: {
type: "String",
unique: true,
required: true,
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = Item = mongoose.model("item", ItemSchema);
Category.js 模型
const mongoose = require("mongoose");
const CategorySchema = new mongoose.Schema({
name: {
type: "String",
required: true,
unique: true,
},
price: {
type: "String",
},
});
module.exports = Category = mongoose.model("category", CategorySchema);
所以据我所知,如果我创建一个项目,然后如果我创建这样的类别:
router.post("/:name", async (req, res) => {
let category = new Category({
name: req.params.name,
});
await category.populate("category").save();
res.send(category);
});
我应该能够对项目提出请求,并且我也应该能够看到类别?因为现在如果我向项目发出获取请求,我只能看到项目属性。
解决方案
推荐阅读
- microservices - 在 ABAC 模型中从集合中过滤被拒绝的项目
- php - 不能使用 Symfony\Component\HttpFoundation\Session\Session 类型的对象作为数组(Symfony 5.3.9)
- python - 仅为 Django-admin 中的实例对象禁用可点击链接
- javascript - jQuery Chosen:如何从多项选择中获取所选选项文本的数组
- datepicker - 我如何阻止日期选择器 vuetify 组件表的 touchend 事件?
- javascript - 单击 React 中的链接后,如果不刷新,则不会发生路由到另一个页面
- azure - Azure Flink 检查点到 Azure 存储:找不到帐户的凭据
- mongodb - 根据 MERN 应用程序中的类别过滤查询以获取产品?
- flutter - 为什么我的耀斑动画只在第一次播放?
- java - Java8:Javers 对象比较 - 如何不列出新创建对象的子元素?