1

I get an error when requesting an API and it crashes on any win server. Can anybody help, please?

This is part of my code:

app.get("/js/2806/api/products/getAllDrugs", (req, res) => {


    const connection = getDBConnection()

    const queryString = "SELECT * FROM tbl_drug";

    connection.query(queryString, (err, rows, fields) => {

        if (err) {
            console.log("Failed to query for drugs: " + err);
            // res.sendStatus(500);
            res.json({
                ErrorDetail: {
                    code: -1,
                    msg: err.code
                }
            });
            res.end();
            return
        }

        const drugs = rows.map((row) => {
            return {
                id: row.id,
                storeId: row.drugStoreId,
                drugName: row.drugName,
                description: row.drugDsc,
                drugUsing: row.drugUsing,
                drugDoseId: row.drugDoseId,
                categoryId: row.categoryId
            };
        })

        res.json({
            ErrorDetail: {
                code: 0
            },
            Response: drugs
        })
    })


});

Error: Connection lost: The server closed the connection. at Protocol.end (C:\Users\Administrator\AppData\Local\CaptainCure\n s\mysql\lib\protocol\Protocol.js:112:13) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:97:28) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:502:10) at Socket.emit (events.js:194:15) at endReadableNT (_stream_readable.js:1125:12) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at Connection._handleProtocolError (C:\Users\Administrator\AppData\ ainCure\node_modules\mysql\lib\Connection.js:425:8) at Protocol.emit (events.js:189:13) at Protocol._delegateError (C:\Users\Administrator\AppData\Local\Ca node_modules\mysql\lib\protocol\Protocol.js:390:10) at Protocol.end (C:\Users\Administrator\AppData\Local\CaptainCure\n s\mysql\lib\protocol\Protocol.js:116:8) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:97:28) I— lines matching original stack trace ...] at process._tickCallback Cinternal/process/next_tick.js:63:19> [nodemon I - wait in is (nodemon] si ing 'node app.js -erver is running on port: 3000 ode events.js:174 throw er; // Unhandled 'error' event is (nodemon] starting 'node app.js' -erver is running on port: 3000

Rashomon
  • 5,962
  • 4
  • 29
  • 67
Alireza Jaba
  • 43
  • 1
  • 2
  • 6
  • 1
    can you show us what is getDBConnection() function returning? – R.Sarkar Apr 01 '19 at 07:36
  • @R.Sarkar `function getDBConnection(){ return mySql.createConnection({ host: 'localhost', user: 'root', password: '***', database: 'captaincure' }) }` – Alireza Jaba Apr 03 '19 at 13:03
  • Possible duplicate https://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection hope this will help you – R.Sarkar Apr 10 '19 at 05:52

1 Answers1

6

Because of the server is already running on back so we do have to restart. and if your using both iterm and the terminal so you need to make sure that the you use only one other one you have to close.