Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Mongoose - Schema with nested SchemaId array

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to add the id field.

There is many other post about this error seen here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!