【问题标题】:Data not showing in the API (NodeJS + Express)API 中未显示数据(NodeJS + Express)
【发布时间】:2021-03-29 18:58:59
【问题描述】:

我正在为餐厅开发一个示例 API,我希望通过输入餐厅的名称来检索餐厅的数据。控制器、模型和路由器已设置,但如果我将其加载到 Postman 中,数据不会出现。请分享一些想法。

控制器:(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
        });
    });
}

型号:(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');

路由器:

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

const router = express.Router();

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

module.exports = router;

【问题讨论】:

  • 你能在你传入url的控制台中看到名字req.params.name吗?
  • 是的,名称显示在控制台中
  • console.log(result) 在您的 .then() 处理程序中记录了什么?
  • 名字也在里面。
  • 什么意思?它应该记录所有找到的与给定名称匹配的文档的数组。

标签: node.js mongodb api express mongoose


【解决方案1】:

如果是,您是否添加了网络访问权限,然后使用Model.create(req.body(err,data)={ res.send(data); })

【讨论】:

  • 在哪里?在模型、控制器或路由器中?
  • app.post('/post',(req,res)=>{ Model.create(req.body(err,data)={ res.send(data); }) })
【解决方案2】:

更改以下内容,我希望它会起作用。 在餐厅模型中,删除此:

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

并添加:

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

在您的餐厅控制器中,将导入脚本更改为:

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

方法是:

Restaurant.find({
    name: Name,
  })

根据需要更改变量和文件名。

【讨论】:

  • 您是否正确更改了名称。我根据您的数据库更新了名称。再试一次。
  • 改了正确但还是不行
【解决方案3】:

我认为问题可能出在从架构中创建模型时

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

用上面的代替

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

那么它应该可以正常工作。

【讨论】:

  • 这个答案和下面的答案完全一样。
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2013-09-16
  • 2019-05-21
  • 2021-09-27
  • 1970-01-01
  • 2013-11-06
相关资源
最近更新 更多