簡體   English   中英

ZCCADCDEDB567ABAE643E15DCF0974E503Z - 帶有嵌套 SchemaId 數組的模式

[英]Mongoose - Schema with nested SchemaId array

我有一個創建餐廳的方法:

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);

它有一系列 Food 模式:

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 object 或其他東西。

您必須添加 id 字段。

這里有很多關於這個錯誤的帖子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM