我已经构建了一个卑鄙的应用程序但是它在发布数字值时遇到了问题。我不确定它是否是一个猫鼬验证错误,但由于某种原因,mongoose不能插入数字值,但是当它是一个字符串时。
这是路线:
//Edit A Site
router.put('/api/sites/:site_id', function(req, res) {
Site.findById(req.params.site_id, function(err, site) {
if (err) {
res.send(err);
} else {
if(req.body.ip) site.ip = req.body.ip;
if(req.body.domain) site.domain = req.body.domain;
if(req.body.wp) site.wp = req.body.wp;
if(req.body.host_name) site.host_name = req.body.host_name;
if(req.body.hosted) site.hosted = req.body.hosted;
console.log(req.body);
// save the site
site.save(function(err) {
if (err)
res.send(err);
res.json(site);
});
}
});
});
console.log具有完整的请求正文:
{ hosted: 1, host_name: 'sup', wp: 'n/a' }
但这是猫鼬的反应:Mongoose: sites.update({ _id: ObjectId("57a16c4a7f7e5b7a7e1f5ad1") }, { '$set': { host_name: 'sup', wp: 'n/a' } })
架构:
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var sitesEntrySchema = new Schema({
ip: {
type: String,
required: true,
trim: true
},
domain: {
type: String,
required: true,
trim: true
},
wp: {
type: String,
required: true,
trim: true
},
host_name: {
type: String,
required: true
},
hosted: {
type: Number,
min: 0,
max: 1,
required: true
}
});
// make this available to our users in our Node applications
var Site = mongoose.model('Site', sitesEntrySchema);
module.exports = Site;
修改 我相信我找到了解决方案。检查req.body.hosted时,因为它是一个数字,它失败了。我必须更新以检查undefined:
if(req.body.hosted != undefined) site.hosted = req.body.hosted;