再會!
發票模型嵌套了Item陣列,我正在使用newItem.ejs 中的req.body實體化新的 Item 物件,但即使req.body不為空,Mongoose 也回傳空物件。
任何意見,將不勝感激!
索引.js
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const Invoice = require('./model/invoice');
const Item = require('./model/invoice');
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.post('/invoice/:id/newItem', (req, res) => {
const { id } = req.params;
const newItem = new Item(req.body);
console.log(req.body); // returns {barcode: '666666', vendorRef: 'test', name: 'test', color: 'test' size: '6', cost: '6', quantity: '6'}
console.log(newItem); // returns { _id: new ObjectId("61d18085424a77e2159b4e38"), items: [] }
Invoice.findByIdAndUpdate(
{_id: id},
{$push: { items: newItem}}).
then(() => {
console.log("Item Added")
})
.catch((err) => {
console.log('Error');
console.log(err);
})
res.redirect(`/search/${id}`);
})
上面的注釋表明req.body不為空,但物件只回傳了 id。
模型
const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
barcode: Number,
vendorRef: String,
name: String,
color: String,
size: Number,
cost: Number,
quantity: Number
})
const invoiceSchema = new mongoose.Schema({
invoiceNumber: {
type: Number,
required: true
},
invoiceDate: {
type: Date,
required: true
},
vendor: {
type: String,
required: true
},
store: {
type: String,
required: true
},
onDelivery: Boolean,
deliveryDate: Date,
received: Boolean,
receivedDate: Date,
posUpdated: Boolean,
posUpdatedDate: Date,
items: [itemSchema]
})
const Item = mongoose.model('Item', itemSchema);
const Invoice = mongoose.model('Invoice', invoiceSchema);
module.exports = Item;
module.exports = Invoice;
新專案.ejs
<form action="/invoice/<%= invoice._id %>/newItem" method="post">
<label for="barcode">Barcode</label>
<input type="number" id="barcode" name="barcode"> <br>
<label for="vendorRef">VendorRef</label>
<input type="text" id="vendorRef" name="vendorRef"> <br>
<label for="name">Name</label>
<input type="text" id="name" name="name"> <br>
<label for="color">Color</label>
<input type="text" id="color" name="color"> <br>
<label for="size">Size</label>
<input type="number" id="size" name="size"> <br>
<label for="cost">Cost</label>
<input type="number" id="cost" name="cost"> <br>
<label for="quantity">Quantity</label>
<input type="number" id="quantity" name="quantity"> <br>
<button>Add</button>
</form>
uj5u.com熱心網友回復:
您從 invoice.js 檔案中匯出值的方式不正確。您正在將 的值重新分配module.exports給 Invoice,這就是為什么只匯出該架構的原因。
因此,您將錯誤的模型匯入到Item. 您可以將Item模式分離到它自己的檔案中,這樣可以更好地構建事物,即。將 Item 架構的代碼移動到item.js
// file: models/item.js
const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
barcode: Number,
vendorRef: String,
name: String,
color: String,
size: Number,
cost: Number,
quantity: Number
});
const Item = mongoose.model('Item', itemSchema);
module.exports = {Item, itemSchema};
// file: models/invoice.js
const mongoose = require('mongoose');
const {itemSchema} = require("./item.js");
const invoiceSchema = new mongoose.Schema({
invoiceNumber: {
type: Number,
required: true
},
invoiceDate: {
type: Date,
required: true
},
vendor: {
type: String,
required: true
},
store: {
type: String,
required: true
},
onDelivery: Boolean,
deliveryDate: Date,
received: Boolean,
receivedDate: Date,
posUpdated: Boolean,
posUpdatedDate: Date,
items: [itemSchema]
})
const Invoice = mongoose.model('Invoice', invoiceSchema);
module.exports = {Invoice, invoiceSchema};
// file: index.js
const { Invoice } = require('./model/invoice');
const { Item } = require('./model/item');
或者
您可以在一行中從同一檔案中解構架構
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const { Invoice, Item }= require('./model/invoice'); // This line
// const = require('./model/invoice');// Get rid of this
const bodyParser = require('body-parser')
為此,您還必須將匯出行更改invoice.js為
module.exports = {Item, Invoice}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401492.html
標籤:javascript 节点.js 表达 猫鼬 ejs
