邮差发布请求中未插入猫鼬模型密钥(Mongoose model keys not being inserted in Postman post request)
我试图使用下面的post方法来创建新文档。 但是当我在Postman中发送一个post请求(例如http:// localhost:3000 / api / posts?title = HeaderThree )时,会创建一个新文档,但是键和值都会丢失。
router.route('/posts') .get(function(req, res) { Post.find(function(err, posts) { if (err) { return res.send(err)} res.json(posts) }) }) .post(function(req, res) { const post = new Post( { title: req.body.title, text: req.body.text } ); post.save(function(err, post) { if (err) { return res.send(err)}; res.json({ message: 'Post added!'}); }); });
架构是这样的:
const mongoose = require('mongoose'); const Schema = mongoose.Schema; const PostSchema = new Schema( { date: {type: Date, default: Date.now}, title: {type: String}, text: {type: String }, comments: {type: Array} } ) module.exports = mongoose.model('PostSchema', PostSchema);
I'm trying to use the post method below to create new documents. But when I send a post request in Postman (e.g. http://localhost:3000/api/posts?title=HeaderThree) a new document is created but the keys and values are missing.
router.route('/posts') .get(function(req, res) { Post.find(function(err, posts) { if (err) { return res.send(err)} res.json(posts) }) }) .post(function(req, res) { const post = new Post( { title: req.body.title, text: req.body.text } ); post.save(function(err, post) { if (err) { return res.send(err)}; res.json({ message: 'Post added!'}); }); });
The schema is this:
const mongoose = require('mongoose'); const Schema = mongoose.Schema; const PostSchema = new Schema( { date: {type: Date, default: Date.now}, title: {type: String}, text: {type: String }, comments: {type: Array} } ) module.exports = mongoose.model('PostSchema', PostSchema);
原文:https://stackoverflow.com/questions/48672846
最满意答案
你需要循环结果:
.... if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { //<----- $PART_NUMBER = $row["PART_NUMBER"]; $FSC = $row["FSC"]; $NIIN = $row["NIIN"]; $ITEM_NAME = $row["ITEM_NAME"]; echo $ITEM_NAME, ", " .$PART_NUMBER, ", " .$FSC, ", " .$NIIN; } } else { ...
你应该使用准备好的语句,看看如何在这里: 我如何防止PHP中的SQL注入?
You need to loop the results:
.... if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { //<----- $PART_NUMBER = $row["PART_NUMBER"]; $FSC = $row["FSC"]; $NIIN = $row["NIIN"]; $ITEM_NAME = $row["ITEM_NAME"]; echo $ITEM_NAME, ", " .$PART_NUMBER, ", " .$FSC, ", " .$NIIN; } } else { ...
And you should use prepared statements, take a look how to here: How can I prevent SQL injection in PHP?
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
查询查询 - MySQL和PHP(Query a Query - MySQL and PHP)[2022-07-02]
您可以在同一SQL查询中执行嵌套查询并将PHP保留在其中: 'SELECT * FROM (SELECT * FROM foo WHERE bar="something") AS q1 WHERE q1.bar2 = "something else"' You could do a nested query in the same SQL query and keep PHP out of it: 'SELECT * FROM (SELECT * FROM foo WHERE bar="something ... -
你需要循环结果: .... if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { //<----- $PART_NUMBER = $row["PART_NUMBER"]; $FSC = $row["FSC"]; $NIIN = $row["NIIN"]; $ITEM_NAME = $row["ITEM_NAME"]; ...
-
我建议使用相同的页面显示结果,过滤器是否适用于位置或卧室数量。 您总是可以在SQL中使用WHERE (1 = 1) ,以便可以附加AND (Location='$loc')或AND (Beds='$bed') ,尽管最好使用参数。 完成设置后,您可以修改链接。 假设您的目标网页是webproperty.php 。 将该URL存储在变量$urlDest 。 当您按位置生成链接以进行过滤时,将$_GET中不是loc任何变量添加到$urlDest ,得到类似webproperty.php?bed=2 。 在迭代您 ...
-
php中的mysql多语句(Mysql multi statement in php)[2022-02-01]
MySQL可以处理多个插入语句。 Barmar强调一些连接管理器不处理该构造。 假设SQL发送不变,在$ data的赋值中似乎缺少逗号。 $ data = $ _GET ['data']. "(-1, -1)"; 应该... $data = $ _GET['data'] . ", (-1, -1)"; 当且仅当值$ _GET ['data']不为空时,例如data =“(ID,DISTANCE),(ID,DISTANCE)”。 MySQL can handle multiple insert state ... -
触发return ,函数结束,并且不执行任何其他代码行。 如果要延迟数据的返回直到函数的后期,则创建变量,赋值,并在准备好后返回变量。 例如,您可以调整代码以为$data创建数组,如下所示: function dbGet($conn, $table, $select, $where, $cond) { $data = array(); ... while ( $row = $result->fetch_assoc() ) { $data[] = $row; } ...
-
regionI希望你转发你的查询条目:) 我要做的是使用子查询压缩查询 SELECT * FROM registrar_claims WHERE reg_region IN (SELECT region FROM regions WHERE staff_100_id='$id') AND ready_to_process='yes' ORDER BY claim_id ASC 这应该产生所需的有序结果 regionI hope you are escaping your query entri ...
-
我一直在研究同一主题(搜索关键字)一段时间,这是我做到的: $words = $_POST['keywords']; if(empty($words)){ //redirect somewhere else! } $parts = explode(" ",trim($words)); $clauses=array(); foreach ($parts as $part){ //function_description in my case , replace it with whateve ...
-
我想MDB2可能会以某种方式改变你的查询。 所以要找到它你需要记录在你的数据库中执行的查询(请参阅 : http : //dev.mysql.com/doc/refman/5.1/en/query-log.html以及如何显示最后在MySQL上执行的查询? )。 ...ok I found out what was the problem: I cannot execute multiple sentences in one go. I had to split them into single sente ...
-
mysql SET类型和多数据进程用php在sql查询中(Mysql SET type and multi data process with php in sql query)[2021-12-30]
你正在寻找一个名为'action AND Genre like Adventure'的流派,你应该做些什么 SELECT * FROM animelist WHERE Genre like 'action' OR Genre like 'Adventure' ORDER BY NAME DESC 多数民众赞成你的问题,但你应该使用预备语句来防止SQL注入,并尽可能使用=而不是像。 Okey I solved it by using FIND_IN_SET