不确定为什么 mongo/express 只创建 ID 但不保存任何其他数据
Not sure why mongo/express is only creating ID but not saving any other data
我是 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}
这是我的 mongoose 模型...
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;
有人知道为什么会这样吗?谢谢
编辑:
我也忘记了为我的 Pug 表单添加代码。这是...
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
我的建议是使用异步路由并等待固定资产的创建:
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')
}
});
我想通了。这是我的哈巴狗形式。我忘记将名称属性放入我的表单字段中。菜鸟错误。感谢你们的帮助
我是 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}
这是我的 mongoose 模型...
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;
有人知道为什么会这样吗?谢谢
编辑: 我也忘记了为我的 Pug 表单添加代码。这是...
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
我的建议是使用异步路由并等待固定资产的创建:
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')
}
});
我想通了。这是我的哈巴狗形式。我忘记将名称属性放入我的表单字段中。菜鸟错误。感谢你们的帮助