gpt4 book ai didi

mysql - 使用 MySQL 从 Node API 获取响应数据

转载 作者:行者123 更新时间:2023-11-29 17:00:13 25 4
gpt4 key购买 nike

我正在尝试使用 Node JS API 和 MySQL 从数据库表中获取数据,但无法获取。我在控制台中获取数据,但未返回到 API。

我的代码是 -

const config = require('../../../config/config');
var con = config.con;

module.exports = (app) => {

app.get('/api/counters', (req, res, next) => {
con.query("SELECT * FROM counters", function (err, result, fields) {
if (err) throw err;
.then((counter) => result.json())
.catch((err) => next(err));
});
});
};

最佳答案

为了从数据库中获取数据,您应该删除函数中的 thencatch 函数。即:

app.get('/api/counters', (req, res, next) => {
con.query("SELECT * FROM counters", function (err, result, fields) {
if (err) throw err;
var counters= result.recordset;
});
});

关于mysql - 使用 MySQL 从 Node API 获取响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com