0

I have a method that creates restaurant:

const { restaurant } = req;

        if (!restaurant || !Object.keys(restaurant).length) {
            return { msg: "Not enough data!", status: 500 };
        }
        const restaurantRecord = new Restaurants(restaurant);

        await restaurantRecord.save();

It has the following schema:

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

It has an array of Food schemas:

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

When I try to create restourant in Postman like this:

{
    "restaurant":
    {
        "name": "Test restaurant",
        "menu":
        [
            {"name": "Test food"},
            {"name": "Test food 2"}
        ]
    }
}

i get an error: enter image description here

How can I fix this? I guess it needs me to create Food object or something.

1 Answer 1

1

You have to add the id field.

There is many other post about this error seen here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.