Node.js特定路由未显示SQL查询。

huangapple go评论77阅读模式
英文:

nodejs specific route not displaying sql query

问题

我是新手学习JavaScript和Node.js,我有一个问题

我有一段代码,我正在创建一个简单的API,但我对为什么会发生这种情况感到非常困惑


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

我有这个路由,用于在本地主机的MySQL上为我列出所有用户。连接是有效的,但当我输入该路由时,它返回一个空白屏幕和"[]"(空列表)

控制台和HTML页面没有显示任何错误

但当我将路由更改为"list_all"而不带有"api"部分时,它正常工作。像这样:

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

它会返回SQL响应。

问题是:为什么在路由中包含"api"时不起作用?

"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)
            }
        )
    })
}

我期望"api/list_all"路由能够正常返回包含所有用户的SQL结果。


<details>
<summary>英文:</summary>

I&#39;m new to javascript and nodejs and i have a question

I have a code who i&#39;m working in a simple api and i&#39;m getting a lot of trouble to understand 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 to list all the users for me on screen for a local host mysql. The connection are working but when i type the route, returns me a blank screen with &quot;[]&quot; (empty list)

The console and the html page does not display any errors

But when i change the route for just &quot;list_all&quot; without the &quot;api&quot; part, it goes well. Like this:


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


It returns me with the SQL response.

The question is: why is not working when i give the &quot;api&quot; on route?

The function &quot;get_info&quot;:

function get_info(sql_data, callback){

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

}





I was expecting the route &quot;api/list_all&quot; to return the SQL result with all users normally.

</details>


# 答案1
**得分**: 0

You are probably importing wrong your code to your main server file

 If the code shown above is in a different file from your **server** file you should add in the server.js the line `app.use('/api/list_all', imported_route);` and then change your code to
 
        app.get('/', (req, res) => {
        get_info(`SELECT * FROM users;`, function(result){
            res.json(result);
        });
       });
For more help refer to previous question https://stackoverflow.com/questions/59418197/routing-not-working-in-node-js-and-express-js-api

<details>
<summary>英文:</summary>

You are probably importing wrong your code to your main server file

 If the code shown above is in a different file from your **server** file you should add in the server.js the line `app.use(&#39;/api/list_all&#39;,imported_route);` and then change your code to
 

        app.get(&#39;/&#39;, (req, res) =&gt; {
        get_info(`SELECT * FROM users;`, function(result){
            res.json(result);
        });
       });
For more help refer to previous question https://stackoverflow.com/questions/59418197/routing-not-working-in-node-js-and-express-js-api

</details>



huangapple
  • 本文由 发表于 2023年4月4日 17:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定