姓名:“薩爾曼,
蛋白質:23,
碳水化合物:23,
卡路里:1221,
脂肪:12,
配料:{
鹽:1茶匙,
......
我想要一個這樣的模式我怎樣才能用 nodejs mongoose 制作這個模式,我怎樣才能在這個嵌套模式中添加資料因為我收到錯誤
uj5u.com熱心網友回復:
最初,您必須像這樣創建表架構:
const mongoose = require('mongoose');
// Health Schema
const HealthSchema = new mongoose.Schema({
name: {
type: String,
},
protein: {
type: String,
},
carbs: {
type: String,
},
calorie: {
type: String,
},
fats: {
type: String,
},
ingredients: {
type: object,
default: {
salt: '',
fibre: ''
}
},
});
module.exports = mongoose.model(
'health',
HealthSchema
);
這里的成分物件已經預先設定了鍵名。所以你只需要在創建時傳遞值。
uj5u.com熱心網友回復:
我會在這里跳過很多東西。
在你的架構中做一些類似的事情,
ingredients:[String]
創建新資料時,請執行以下操作
const healtData= await new healtData({... the data here})
在呼叫 save 之前做這樣的事情
healthData.ingredients.push(ingredient)
healthData.save()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364403.html
上一篇:為什么順序在中間件檔案中很重要?
