我有一個創建餐廳的方法:
const { restaurant } = req;
if (!restaurant || !Object.keys(restaurant).length) {
return { msg: "Not enough data!", status: 500 };
}
const restaurantRecord = new Restaurants(restaurant);
await restaurantRecord.save();
它具有以下架構:
const mongoose = require("mongoose");
const { Food } = require("./food");
const RestaurantsSchema = mongoose.Schema(
{
name: { type: String, index: true, unique: true, default: "Test queue" },
menu: [{ type: mongoose.Schema.Types.ObjectId, ref: Food, index: true, default: [] }]
},
{ collection: "restaurants" }
);
module.exports.Restaurants = mongoose.model("Restaurants", RestaurantsSchema);
它有一系列食物模式:
const mongoose = require("mongoose");
const FoodSchema = mongoose.Schema(
{
name: { type: String, index: true, unique: true, default: "Some food" },
price: { type: Number, index: true, default: 100 },
},
{ collection: "food" }
);
module.exports.Food = mongoose.model("Food", FoodSchema);
當我嘗試像這樣在 Postman 中創建餐廳時:
{
"restaurant":
{
"name": "Test restaurant",
"menu":
[
{"name": "Test food"},
{"name": "Test food 2"}
]
}
}
我收到一個錯誤:

我怎樣才能解決這個問題?我想它需要我創建 Food 物件或其他東西。
uj5u.com熱心網友回復:
您必須添加 id 欄位。
還有許多其他職位有關此錯誤看到這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/387317.html
標籤:javascript 猫鼬 邮差
上一篇:貓鼬索引html文本?
