简体   繁体   中英

Data not showing in the API (NodeJS + Express)

I am developing a sample API for restaurants in which I want retrieve data of restaurants by entering the Restaurant's name. Controller, Model & Router has been set but if I load it into Postman data doesn't appear. Please share some ideas.

Controller: (restaurant.js)

const restaurants = require('../Models/restaurantData');
exports.getRestaurantName = (req, res) => {
    const Name = req.params.name;    
    restaurants.find({
        name: Name
    }).then(result => {
        res.status(200).json({
            message: "Restaurant Data",
            restaurants: result[0]
        });
    }).catch(error => {
        res.status(500).json({
            message: error
        });
    });
}

Model: (restaurantData.js)

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const restaurantSchema = new Schema({
    _id: {
        type: Number,
        required: true
    },
    name: {
        type: String,
        required: true
    },
    city_name:{
        type: String,
        required: true
    },
    city_id: {
        type: String,
        required: true
    },
    location_id: {
        type: Number,
        required: true
    },
    area: {
        type: Number,
        required: true
    },
    locality:{
        type: String,
        required: true
    },
    thumb: {
        type: String,
        required: true
    },
    cost:{
        type: Number,
        required: true
    },
    address:{
        type: String,
        required: true
    },
    mealtype:{
        type: Number,
        required: true
    },
    name:{
        type: String,
            required: true
        },    
    cuisine:{
        type: Number,
        required: true
    },
    type:{
        type: Array,
        required: true
    },
    Cuisine:{
        type: Array,
        required: true
    }
});

module.exports = mongoose.model('restaurantData', restaurantSchema, 'restaurantData');

Router:

const express = require('express');
const restaurantController = require('../Controllers/restaurant');

const router = express.Router();

router.get('/restaurantData/:name',restaurantController.getRestaurantName);

module.exports = router;

Change the following and I hope it will work. In Restaurant Model, remove this:

module.exports = mongoose.model(  "restaurantData",  restaurantSchema,  "restaurantData");

And add this:

const Restaurant = mongoose.model("restaurantData", restaurantSchema);
module.exports = { Restaurant };

In your Restaurant controller, change the import script into:

const { Restaurant } = require("../Models/restaurantData");

And Method will be:

Restaurant.find({
    name: Name,
  })

Change the variable and file names according to your need.

had you add network access if yes then use Model.create(req.body(err,data)={ res.send(data); })

I think the problem might be while creating the model out of the schema

module.exports = mongoose.model('restrauntData', restaurantSchema);   

use the above instead of

module.exports = mongoose.model(  "restaurantData",  restaurantSchema,  "restaurantData");

then it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM