connection.query(...).then不是一个函数 [英] connection.query(...).then is not a function
问题描述
我是Node js的新手.我在后端使用带有express的节点,而mysql作为数据库使用.我在处理异步调用时感到困惑.在我使用的代码中.
I am new to the node js . I am using node with express in my backend and mysql as database . I have the confusion while handling the async calling . In my code while i use .
return connection.query(qry).then(
function(result) {
console.log('Query executed');
resolve(result.rows[0]);
},
function(err) {
console.log('Error occurred', err);
reject(err);
}
)
我收到错误 connection.query(...).然后不是函数
connection.query(qry, function (err, rows, fields) {
});
已正确执行. 简单的查询我没有任何问题.在执行复杂查询时,上面的一个不是等待完整的查询执行
is executed correctly. Simple query i am not getting any problem . while execute the complex query the above one is not wait for complete query execution
推荐答案
我还没有发现,我们可以在connection.query('')
函数中使用promise.根据mysqljs文档,我们可以传递回调函数以获取结果.
I have not found that, we can use promise with connection.query('')
function. As per mysqljs documentation we can pass callback function for getting result.
var mysql = require('mysql');
var connection = mysql.createConnection(...);
connection.query('SELECT * FROM table_name', function (error, results, fields) {
if (error) throw error;
console.log(result);
});
这篇关于connection.query(...).then不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!