教程中的代码
//获取数据库内容 router.get('/api/goods/search', function(req,res,next){ connection.query("select * from goods_search", function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results); }); });
无法连接,需要优化,正常的代码
router.get('/api/goods/search', function (req, res, next) { connection.query('SELECT * FROM goods_search', function (error, results, fields) { if (error) { console.error('Error executing query:', error); return res.status(500).json({ error: 'Database error' }); } console.log('The solution is: ', results); // 发送响应 res.json({ message: 'Query successful', data: results, }); }); });
输出数据
router.get('/api/goods/search', async function (req, res, next) { let result = await connection.query('SELECT * FROM goods_search'); res.json({ message: 'Query successful', data: result, }); });
标签:function,uniapp,search,goods,res,数据库,results,error,连接 From: https://www.cnblogs.com/qianjilou/p/17626099.html