保存到数据库后永远等待本地主机



当我想在提交chrom时保存到数据库时,给我按摩等待本地主机,这是永远的。 我如何解决这个问题。数据被保存,一切似乎都很好,哪里有问题......请帮忙...enter code here

const bodyParser = require('body-parser');
const mysql = require('mysql');
var urlencodedParser = bodyParser.urlencoded({extended: false});

var connection = mysql.createConnection({
host:'127.0.0.1',
user:'root',
password: '',
database: 'sm_db'
});
connection.connect(function(err){
if(!err){
console.log('CONNECTED');
} else{
console.log('ERROR');
}
});
app.post('/', urlencodedParser, function(req, res){
const germant = req.body.germant;
const germantMsqlData = {germant};
connection.query('INSERT INTO summersidedatatable SET ?', germantMsqlData, function (error, results, fields) {
if (error) {
res.send('SEND GERMANT DATA ERROR');
}
});
});
};

您的错误是如果没有错误,则不会发送响应:

app.post('/', urlencodedParser, function(req, res){
const germant = req.body.germant;
const germantMsqlData = {germant};
connection.query('INSERT INTO summersidedatatable SET ?', germantMsqlData, function (error, results, fields) {
if (error) {
res.send('SEND GERMANT DATA ERROR');
}
//here you are missing something
res.send('Done with query');
});
});

您错过了完成请求,那么您的浏览器将永远等待

app.post('/', urlencodedParser, function(req, res){
const germant = req.body.germant;
const germantMsqlData = {germant};
connection.query('INSERT INTO summersidedatatable SET ?', germantMsqlData, function (error, results, fields) {
if (error) {
return res.send('SEND GERMANT DATA ERROR'); // finish the request in error case 
}
res.end(); // finish the request in success case, just end the request, does not send any data back to client.
});
});

替换

const germantMsqlData = {germant};

使用以下代码。请提供您的数据库字段名称

const germantMsqlData={
"data_base_field_name":germant
}

在这里,您提到了未提及的错误部分成功部分。 res.send((;

试用我的代码

app.post('/', urlencodedParser, function(req, res){
const germant = req.body.germant;
const germantMsqlData={
"data_base_field_name":germant
}
connection.query('INSERT INTO summersidedatatable SET ?', germantMsqlData, function (error, results, fields) {
if (error) {
res.send('SEND GERMANT DATA ERROR');
}
res.send('Redirect');
});
});

将data_base_field_name替换为字段名称

最新更新