SQL query not showing up in specific route in Node.js
P粉770375450
P粉770375450 2024-04-04 00:24:22
0
1
503

I am new to javascript and nodejs and I have a question

I have a code that I'm working on in a simple API but I'm having a hard time understanding why this is happening

app.get('/api/list_all', (req, res) => {
    get_info(`SELECT * FROM users;`, function(result){
        res.json(result);
    });
});

I have this route that lists all users of localhost mysql on the screen. The connection works fine but when I enter directions it returns a blank screen with "[]" (empty list)

The console and html page do not show any errors

But when I only change the route of "list_all" without the "api" part, everything goes fine. like this:

app.get('/list_all', (req, res) => {
    get_info(`SELECT * FROM users;`, function(result){
        res.json(result);
    });
});

It returns me the SQL response.

The question is: Why doesn't it work when I provide "api" on the route?

Function "get_info":

function get_info(sql_data, callback){

    con.getConnection((error, conn) => {
        if (error) { throw error; }
        conn.query(
            sql_data,
            (error, resultado) => {
                conn.release();
                if (error) { throw error; }
                return callback(resultado)
            }
        )
    })
}

I hope that route "api/list_all" can return SQL results for all users normally.

P粉770375450
P粉770375450

reply all(1)
P粉038161873

You may have imported the wrong code into the main server file

If the code shown above is in a different file than your server file, you should add the line app.use('/api/list_all',imported_route) in server.js ; Then change the code to

app.get('/', (req, res) => {
    get_info(`SELECT * FROM users;`, function(result){
        res.json(result);
    });
   });

For more help, see the previous question Routing does not use the Node.js and Express.js API

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template