我是Express / Node / Mongo的新手,正在尝试构建资产数据库应用程序来存储固定资产。
我正在尝试通过Web表单保存固定资产数据,但是出于某种原因仅创建了ID,并且表单中的实际数据没有保存。
我已经查看了mongo容器中的数据,并且只能看到我创建的每个资产的ID
这是我的路线...
/* POST create fixed-assets page. */
router.post('/financial/assets/create-fixed-asset', secured(), function(req, res, next) {
var assetData = {
model_number: req.body.model_number,
manufacturer: req.body.manufacturer,
description: req.body.description,
serial_number: req.body.serial_number,
asset_tag_number: req.body.asset_tag_number,
condition_when_purchased: req.body.condition_when_purchased,
price_paid: req.body.price_paid
};
FixedAsset.create(assetData, function (error, asset) {
if (error) {
return next(error);
} else {
res.redirect('/financial/assets/fixed-assets');
}
});
});
这是我的列表视图...(使用Pug / Jade)
block view
.animated.fadeIn
h1 Fixed Assets
a(href='create-fixed-asset/') Create
br
br
table#example.display
thead
tr
th ID
th Model
th Description
tbody
each asset in assets
tr
td #{asset.id}
td #{asset.model_number}
td #{asset.manufacturer}
这是我的猫鼬模特...
var mongoose = require('mongoose');
var FixedAssetSchema = new mongoose.Schema({
model_number: {
type: String
},
manufacturer: {
type: String
},
description: {
type: String
},
serial_number: {
type: String
},
asset_tag_number: {
type: Number
},
condition_when_purchased: {
type: String
},
price_paid: {
type: Number
}
});
var FixedAsset = mongoose.model('FixedAsset', FixedAssetSchema);
module.exports = FixedAsset;
有人知道为什么会这样吗?谢谢
编辑: 我也忘记为帕格表格输入代码。在这里...
extends /default
block scripts
if !starter
// Plugins and scripts required by this view
script(src='/js/main.js')
block view
.animated.fadeIn
.container.row
.col-md-6
h1 #{title}
.container.row
.col-md-6
form(method='POST' action='/financial/assets/create-fixed-asset')
div.form-group
input#model_number.form-control(type='text', placeholder='Model Number')
div.form-group
input#manufacturer.form-control(type='text', placeholder='Manufacturer')
div.form-group
input#serial_number.form-control(type='text', placeholder='Serial Number')
div.form-group
input#description.form-control(type='text', placeholder='Description')
div.form-group
input#asset_tag_number.form-control(type='text', placeholder='Asset Tag Number')
div.form-group
input#condition_when_purchased.form-control(type='text', placeholder='Condition When Purchased')
div.form-group
input#price_paid.form-control(type='text', placeholder='Price Paid')
br
button.btn.btn-success(type='submit') Create
答案 0 :(得分:0)
我的建议是使用异步路由并等待固定资产的创建:
router.post('/financial/assets/create-fixed-asset', secured(),async function(req, res, next) {
try{
var assetData = {
model_number: req.body.model_number,
manufacturer: req.body.manufacturer,
description: req.body.description,
serial_number: req.body.serial_number,
asset_tag_number: req.body.asset_tag_number,
condition_when_purchased: req.body.condition_when_purchased,
price_paid: req.body.price_paid
};
await FixedAsset.create(assetData, function (error, asset) {
if (error) {
return next(error);
} else {
res.redirect('/financial/assets/fixed-assets');
}
});
}
catch(err){
res.redirect('/somewhere else/ a 404 page')
}
});
答案 1 :(得分:0)
我知道了。那是我的帕格形式。我忘记了将name属性放在表单字段中。菜鸟的错误。谢谢你们的帮助