0

I have a model which represents a menu for a restaurant, within that model I have an array for the items in the menu. I am not sure how to add menu items to my menu model using express, I bolded the line where I am not sure what to write.

This is the menu model:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const FoodItemSchema = new Schema({
  name: { type: String, required: true },
  price: { type: Number, required: true },
  Category: { type: String, required: false },
  Quantity: { type: String, required: false },
});

const MenuSchema = new Schema({
  restaurant_id: { type: mongoose.Schema.Types.ObjectId, ref: "restaurant" },
  items: [FoodItemSchema],
  dateCreated: { type: String, required: false },
});

module.exports = mongoose.model("menu", MenuSchema);

And this is my express function which I am using to add menus to my database, I am unsure what to add for the array of menu items within the function.

exports.sendMenuData = (req, res) => {
  const menu = new Menu({
    restaurant_id: req.body.restaurant_id,
    **items: [FoodItemSchema]**, //Not sure what to write here in terms of req 
    dateCreated:req.dateCreated,
  });
  menu
    .save()
    .then((data) => {
      console.log(data);
      res.send(data);
    })
    .catch((err) => {
      console.log(err);
    });
};
1
  • What are you sending in the request? I would assume some kind of JSON data that has the food items? Commented Oct 13, 2020 at 20:48

1 Answer 1

1

You need to pass through postman or whatever app you are using to acces your db the array of food items.

Example:

items: [req.body.theNameYouWant],

In postman:

theNameYouWant: {name:"Hot dog",price: 3,...(You must place here all the atributes of FoodItemSchema)}

Otherwise, you can also use operators such $addToSet $push which will allow you to introduce FoodItemSchema in the array.

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

2 Comments

Thank you for the help, what do I do if I want to add more than one food item at a time using postman.
You can use , after } and then open again brackets.

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.