Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

node.js - Cannot POST /updatedata

1.I am trying to modify my data in mysql database but getting error

  1. I am able to add the data but unable to modify the data.

  2. I have created a seperate tab in main page as modify. What changes needs to be done?


**var mysql = require('mysql');
var express = require('express');
var bodyParser = require('body-parser');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'mr',
    port: '3308',
});
var app = express();
app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());
app.get('/', function(request,response){
    response.sendFile(__dirname + '/home.html');
});
app.get('/update', function(request, response){
    response.sendFile(__dirname + '/modify.html');
});
app.put('/updatedata',function(req,res){
    
    connection.query('update hp set Name=?,Address=?,Country=?,Phone=? where Id=?',[req.body.Name,req.body.Address,req.body.Country,req.body.Phone,req.body.Id],function(error,results,fields){
        if(err) throw err;
        console.log("Record updated");
        res.redirect('/home');
    });
});
app.get('/home', function(request, response) {
    
    response.sendFile(__dirname + '/home.html');
});
app.listen(5000);
console.log('Server Started');**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The title indicates you do a POST on /updatedata, but you define the PUT method for /updatedata in your code. Try to replace app.put('/updatedata' with app.post('/updatedata'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...