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

Categories

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

mysql - POST http://localhost:3307/insert 500 (Internal Server Error)

I'm trying to add a client here and I get this error. POST http://localhost:3307/insert 500 (Internal Server Error) . I don't know exactly what is the problem. Maybe I make something wrong in my code, or maybe is from something else.
https://prnt.sc/wfueig

const express = require("express");
const mysql = require("mysql");
const cors = require("cors");
const { listenerCount } = require("process");

const app = express();

app.use(express.json());
app.use(cors());

//Int a = 1;


const db = mysql.createConnection({
    user: "root",
    host: "localhost",
    password: "123456",
    database: "clau"
    //port: "1521",
});

app.post('/register', (req, res) => {

const username = req.body.username
const password = req.body.password

db.query(
    "INSERT INTO USERS (username,password) VALUES(?,?);",
    [username, password],
    (err, result) => {
            console.log(err); 
   
}
);
});

app.post('/login', (req, res) => {
    const username = req.body.username
const password = req.body.password

db.query(
    "SELECT * FROM USERS WHERE username = ? and password = ?",
    [username, password],
    (err, result) => {
        if(err) {
            res.send({err: err});
        }
        if(result.length > 0) {
                res.send(result);
            } else {
                res.send({ message: "Wrong username/password combination!"});
            }
        }
);

});

class DbService{
    static getDbServiceInstance(){
        return instance ? instance: new DbService();
    }

    async insertNewClient(firstname,lastname,phone,email,city) {
        try{
            const insertId = await new Promise((resolve,reject) => { 
                const query = "INSERT INTO client (firstName,lastName,Phone,Email,City) VALUES(?,?,?,?,?);";

                resolve()
                connection.query(query,[firstname,lastname,phone,email,city],(err,result)=>{
                    if (err) reject(new Error(err.message));
                    resolve(result.insertId);
                })
            });

            //console.log(insertId);
            return response;
        }
        catch(error)
        {
            console.log(error);
        }
    }

}
app.post('/insert', (request, response) => {
    const { firstname, lastname, phone, email, city } = request.body;
    console.log("in insert");
    db.query(
        "INSERT INTO client (firstName,lastName,Phone,Email,City) VALUES(?,?,?,?,?);",
        [firstname, lastname, phone, email, city],
        (err, result) => {
            console.log(err);

        }
    );
    const db = dbService.getDbServiceInstance();

    const result = db.insertNewName(firstname,lastname,phone,email,city);

    result
        .then(data => response.json({ success: true }))
        .catch(err => console.log(err));
});


app.listen(3307, () => {
    console.log("Running server ");
});

(****************This is for requested details spam *********** OFF TOPIC)

When you picture mountain climbers scaling Mount Everest, what probably comes to mind are teams of climbers with Sherpa guides leading them to the summit, equipped with oxygen masks, supplies and tents. And in most cases you'd be right, as 97 per cent of climbers use oxygen to ascend to Everest's summit at 8,850 metres above sea level. The thin air at high altitudes makes most people breathless at 3,500 metres, and the vast majority of climbers use oxygen past 7,000 metres. A typical climbing group will have 8–15 people in it, with an almost equal number of guides, and they'll spend weeks to get to the top after reaching Base Camp.

But ultra-distance and mountain runner Kilian Jornet Burgada ascended the mountain in May 2017 alone, without an oxygen mask or fixed ropes for climbing.

Oh, and he did it in 26 hours.

With food poisoning.

question from:https://stackoverflow.com/questions/65560503/post-http-localhost3307-insert-500-internal-server-error

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

1 Reply

0 votes
by (71.8m points)

You should have to move your const db = dbService.getDbServiceInstance(); from line 96 to line 87.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...