我是后端開發的新手,通過使用node js和MongoDB創建了一個post API,
這是我的模式模型
這是我的模式模型。
這是我的模式模型
// 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: 字串,
// },
dishRating: {
type: String。
},
}
]
});
const restaurantSchema = new mongoose.Schema({
restaurantName: {
type: String,
},
restaurantRating:{
type: String.
},
restaurantAddress: {
type: Stringcategory: [
{
type: mongoose.Schema.Types.ObjectId。
ref。'foodSchema'。
}
]
})
const Restaurant = new mongoose.model('RESTAURANT', restaurantSchema)。
module.exports = Restaurant;
下面是路由器的代碼
// 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.categoryNamedishName: 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: "食物添加成功" })
} catch (err) {
console.log(err)
}
})
module.exports = router;
這段代碼將進入app.js(主檔案)
當我在Auth.js檔案中注釋出類別(陣列)時,我只得到了這個資料,否則無法在資料庫檔案中保存任何資料。 [1]: https://i.stack.imgur.com/wKn9p.png
uj5u.com熱心網友回復:
首先,這是一個很好的問題,有非常有效的代碼,做得很好!
因此,一切都正確。
因此,除了你應該將你的foodSchema也宣告為mongoose模型外,你的代碼中的所有內容都是正確的,就像你這樣做一樣
const Restaurant = new mongoose。 model('RESTAURANT', restaurantSchema) 。
你不需要使用new,你可以直接省略它。
所以基本上在這旁邊添加你的foodSchema的宣告,它應該是這樣的:
const Restaurant = mongoose. model('RESTAURANT', restaurantSchema) 。
const Food = mongoose.model('FOOD', foodSchema)。
而對于匯出它們,你可以這樣做:
module.exports = {
Restaurant: Restaurant,
食品: 食品: 食品
對于在你的路由器檔案中匯入這兩個中的任何一個,你可以使用析構法:
const { Restaurant } = require('./model/restaurantSchema'/span>)
我希望這對你有幫助,祝你有個愉快的一天!
uj5u.com熱心網友回復:
你已經將類別宣告為mongoose typeId的串列,并插入了foodSchema型別的物件串列。在你的模式檔案中,在宣告了foodSchema之后,使用該食物模式創建一個模型。
const Food = mongoose. model('FOOD'/span>, foodSchema)
現在在餐廳模式中,類別將參考ref: 'Food'。
mongoose期望類別為["61407ce0b6c1fc83d896002e"],但你正在提供
[{
categoryName:{
type: String,
required: true。
},
dish:[...] 。
}]
從模式檔案中匯出食品和餐廳。
module.exports = Food。
為了創建餐廳,首先創建一個食品
。 const food = new Food({ })
categoryName: req.body.categoryName。
dish: [ [ {
dishName: req.body.dishName。
dishDescription: req.body.dishDescription。
dishPrice: req.body.dishPrice。
dishRating: req.body.dishRating.
} ]
})
let foodInserted = await food.save()。
然后將foodInserted._id歸入餐廳的類別。
const restaurant = new Restaurant({
restaurantName: req.body.restaurantName。
restaurantRating: req.body.restaurantRating。
restaurantAddress: req.body.restaurantAddress。
category: [foodInserted._id] 。
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/334237.html
標籤:
上一篇:Discord.jswithES6:SyntaxError:意外的識別符號
下一篇:在React中根據時區改變時間
