Mongoose:直接将JS对象插入到数据库中(Mongoose : Inserting JS object directly into db)

编程入门 行业动态 更新时间:2024-10-25 06:29:40
Mongoose:直接将JS对象插入到数据库中(Mongoose : Inserting JS object directly into db)

好吧,我有一个JS对象,通过AJAX发送到nodejs后端。 我想直接将这个js对象插入到我的mongoose数据库中,因为对象键已经与db模式完美匹配。

我目前有这个(不动态和过于复杂):

app.post('/items/submit/new-item', function(req, res){ var formContents = req.body.formContents, itemModel = db.model('item'), newitem = new itemModel(); newitem.item_ID = ""; newitem.item_title = formContents.item_title; newitem.item_abv = formContents.item_abv; newitem.item_desc = formContents.item_desc; newitem.item_est = formContents.item_est; newitem.item_origin = formContents.item_origin; newitem.item_rating = formContents.item_rating; newitem.item_dateAdded = Date.now(); newitem.save(function(err){ if(err){ throw err; } console.log('saved'); }) res.send('item saved'); });

但是要把它缩小到像这样(性感和动态):

app.post('/items/submit/new-item', function(req, res){ var formContents = req.body.formContents, formContents.save(function(err){ if(err){ throw err; } console.log('saved'); }) res.send('item saved'); });

Ok so I have a JS object that is being POSTed via AJAX to the nodejs backend. I want to insert this js object directly into my mongoose db as the object keys already match up perfectly with the db schema.

I currently have this (not dynamic and overly complex):

app.post('/items/submit/new-item', function(req, res){ var formContents = req.body.formContents, itemModel = db.model('item'), newitem = new itemModel(); newitem.item_ID = ""; newitem.item_title = formContents.item_title; newitem.item_abv = formContents.item_abv; newitem.item_desc = formContents.item_desc; newitem.item_est = formContents.item_est; newitem.item_origin = formContents.item_origin; newitem.item_rating = formContents.item_rating; newitem.item_dateAdded = Date.now(); newitem.save(function(err){ if(err){ throw err; } console.log('saved'); }) res.send('item saved'); });

But want to trim it down to something like this (sexy and dynamic):

app.post('/items/submit/new-item', function(req, res){ var formContents = req.body.formContents, formContents.save(function(err){ if(err){ throw err; } console.log('saved'); }) res.send('item saved'); });

最满意答案

如果你在猫鼬上使用这样一个插件( http://tomblobaum.tumblr.com/post/10551728245/filter-strict-schema-plugin-for-mongoose-js ),你可以在表单中放置一个数组,比如newitem[item_title]和newitem[item_title] newitem[item_abv] - 或item[title]和item[abv]

如果元素匹配,你也可以传递整个req.body 。 MongooseStrict插件会过滤出你的模式中没有明确设置的任何值,但它仍然会将检查类型和验证结果留给猫鼬。 通过在架构中设置适当的验证方法,您将免受任何注入式攻击。

编辑:假设你已经实现了插件,你应该能够使用这个代码。

app.post('/items/submit/new-item', function(req, res){ new itemModel(req.body.formContents).save(function (e) { res.send('item saved'); }); });

If you use a plugin like this with mongoose (http://tomblobaum.tumblr.com/post/10551728245/filter-strict-schema-plugin-for-mongoose-js) you can just put together an array in your form, like newitem[item_title] and newitem[item_abv] -- or item[title] and item[abv]

You could also just pass the whole req.body if the elements match up there. That MongooseStrict plugin will filter out any values not explicitly set in your schema, but it still leaves checking types and validation up to mongoose. With proper validation methods set in your schema, you will be safe from any injection attacks.

EDIT: Assuming you have implemented the plugin, you should be able to use this code.

app.post('/items/submit/new-item', function(req, res){ new itemModel(req.body.formContents).save(function (e) { res.send('item saved'); }); });

更多推荐

本文发布于:2023-04-29 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   对象   JS   Mongoose   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!