gpt4 book ai didi

mysql - req.body 在 postman 上运行时说未定义

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:59 26 4
gpt4 key购买 nike

var express = require('express');
var mysql=require('mysql');
var bodyParser = require('body-parser');

var con=mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"nodedb"
});

var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());


app.post('/reg', function (req, res) {
var name=req.body.user_name;
console.log(name);

con.query("SELECT * FROM users WHERE email = ?", [req.body.email],
function(err,result){
if(result.length > 0){
console.log('mail already exist');
res.json('mail already exist');

}
else{
con.query("INSERT INTO users (`name`,`email`,`contact`,`password`) VALUES (?,?,?,?) ", [req.body.user_name,req.body.email,req.body.contact_no, req.body.user_password],
function(err1,result1){
if(err1){

console.log('failed');
res.json(err1);
}
else{
console.log('1 recoed inserted');
res.json(result1);

}

})
}
})
}

req.body.user_name print undefined in console and null on postman. It is working on web page but not on postman. What should i do now. I looked in many websites but i didn't get answer.

最佳答案

您需要选择正确的标题内容类型:x-www-form-urlencoded

enter image description here

希望对您有所帮助。

关于mysql - req.body 在 postman 上运行时说未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com