Home:ALL Converter>Not able to save my array in to mongodb database

Not able to save my array in to mongodb database

Ask Time:2021-09-22T05:28:52         Author:anilraj

Json Formatter

I am new in backend development, creating a post API by using node js and MongoDB,

This is my schema model

// restaurantSchema.js

const mongoose = require('mongoose');

const foodSchema = new mongoose.Schema({
    categoryName:{
        type: String,
        required: true
    },
    dish: [
        {
            dishName: {
                type: String,
                required: true
            },
            dishDescription: {
                type: String
            },
            dishPrice: {
                type: Number,
                required: true
            },
            // dishImg: {
            //    type: String,
            // },
            dishRating: {
                type: String
            },
        }
    ]
});

const restaurantSchema = new mongoose.Schema({
    restaurantName: {
        type: String,
       
    },
    restaurantRating:{
        type: String
    },

    restaurantAddress: {
        type: String
    },
    category: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'foodSchema'
        }
    ]
})

const Restaurant = new mongoose.model('RESTAURANT', restaurantSchema);

module.exports = Restaurant;

here is the router code

// auth.js


const express = require('express');
const router = express.Router();
require('../db/conn');
const Restaurant = require('../model/restaurantSchema');

const app = express();

router.post('/post-food', async (req, res) => {

    try {
        

        console.log(req.body, "body")
        const restaurant = new Restaurant({
            
            restaurantName: req.body.restaurantName,
            restaurantRating: req.body.restaurantRating,
            restaurantAddress: req.body.restaurantAddress,
            category: [
               {
                categoryName: req.body.categoryName
               },
                {
                    dishName: req.body.dishName,
                    dishDescription: req.body.dishDescription,
                    dishPrice: req.body.dishPrice,
                    dishRating: req.body.dishRating
                }
            ]
        });

        await restaurant.save();
        console.log(restaurant, "save")
        res.status(201).json({ message: "food added successfully" })


    } catch (err) {
        console.log(err)
    }
})

module.exports = router;

this code is going to the app.js (main file)

I am getting only this data while I comment out the category(array) in the auth.js file otherwise not able to save any data in the database file [1]: https://i.stack.imgur.com/wKn9p.png

Author:anilraj,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275754/not-able-to-save-my-array-in-to-mongodb-database
Florian Saqipi :

First of all this is a great question with very valid code, nicely done!\nSo, everything is correct in your code except you should declare your foodSchema as a mongoose model as well, just like you're doing so with this one\nconst Restaurant = new mongoose.model('RESTAURANT', restaurantSchema);\n\nYou don't need to use new, you can just omit it.\nSo basically next to this add the declaration for your foodSchema it should look something like this:\nconst Restaurant = mongoose.model('RESTAURANT', restaurantSchema);\nconst Food = mongoose.model('FOOD', foodSchema)\n\n\nand for exporting them you could do something like this:\nmodule.exports = {\n Restaurant: Restaurant,\n Food: Food\n}\n\nFor importing any of these two in your router file you could use destructuring:\nconst { Restaurant } = require('../model/restaurantSchema')\n\nI hope this helps, have a good day!",
2021-09-22T00:31:23
yy