[英]Data not showing in the API (NodeJS + Express)
我正在為想要通過輸入餐廳名稱檢索餐廳數據的餐廳開發示例 API。 Controller,Model & 路由器已設置,但如果我將其加載到 Postman 數據不會出現。 請分享一些想法。
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');
路由器:
const express = require('express');
const restaurantController = require('../Controllers/restaurant');
const router = express.Router();
router.get('/restaurantData/:name',restaurantController.getRestaurantName);
module.exports = router;
更改以下內容,我希望它會起作用。 在餐廳 Model 中,刪除此:
module.exports = mongoose.model( "restaurantData", restaurantSchema, "restaurantData");
並添加:
const Restaurant = mongoose.model("restaurantData", restaurantSchema);
module.exports = { Restaurant };
在您的餐廳 controller 中,將導入腳本更改為:
const { Restaurant } = require("../Models/restaurantData");
方法將是:
Restaurant.find({
name: Name,
})
根據需要更改變量和文件名。
如果是,您是否添加了網絡訪問權限,然后使用Model.create(req.body(err,data)={ res.send(data); })
我認為問題可能出在從架構中創建 model 時
module.exports = mongoose.model('restrauntData', restaurantSchema);
使用上面的而不是
module.exports = mongoose.model( "restaurantData", restaurantSchema, "restaurantData");
那么它應該可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.