我已经为我的表单上的字段创建了mongoose模式类型数组。我使用like req.body.fieldName将值作为属性进行访问,但是当我对数组执行此查询时,它返回未定义的值,因为它不是键值对。如何在post路由处理程序中解析新创建的模型?
下面是我的模式和post路由处理程序。
const mongoose = require('mongoose');
const { Schema } = mongoose;
const StoreSchema = new Schema({
name : String,
address: {
street: String,
crossStreet: String,
city: String,
state : String,
zipcode : Number,
country: String,
formattedAddress: Array,
location : {
geoType : String,
coordinates: Array
}
}
})
module.exports = mongoose.model('Store', StoreSchema);store.js文件
const router = express.Router();
const Store = require('../models/store');
router.post('/api/store/register', (req, res, next) => {
const store = new Store();
store.address.street = req.body.street;
store.address.crossStreet = req.body.crossStreet;
store.address.city = req.body.city;
store.address.state = req.body.state;
store.address.zip = req.body.zip;
store.address.country = req.body.country;
store.address.formattedAddress = req.body.formattedAddress;
store.location.geoType = req.body.geoType;
store.location.coordinates = req.body.coordinates;
store.save(() => {
console.log('New store data is entered ' + store);
res.redirect('/');
});
})发布于 2018-01-17 12:07:05
步骤1. npm install body-parser --save
步骤2. var bodyParser = require('body-parser')
步骤3. app.use(bodyParser.json());
https://stackoverflow.com/questions/48293297
复制相似问题