tou*_*tpt 3 javascript sqlite node.js
我正在尝试在expressjs app(nodejs)中使用sqlite3
我想创建一个从select语句返回所有结果的函数.该功能将由一条路线调用
var queryGetAll = 'SELECT id, title, description, modified, lat, lng, zoom FROM maps';
function Manager(){
this.db = null;
this.getAll = function(){
var all = [];
this.db.all(queryGetAll, function(err, rows){
if (err){
throw err;
}
all.push.apply(all, rows);
});
return all;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道nodejs是异步的,所以它意味着在查询结束之前调用return.但我没有找到关于如何使用sqlite的例子.
您的示例中的"全部返回"行将在this.db.all()调用您的回调之前执行.为了使您的代码能够工作,您需要执行以下操作:
var queryGetAll = 'SELECT id, title, description, modified, lat, lng, zoom FROM maps';
function Manager(){
this.db = null;
// Allow a callback function to be passed to getAll
this.getAll = function(callback){
this.db.all(queryGetAll, function(err, rows){
if (err){
// call your callback with the error
callback(err);
return;
}
// call your callback with the data
callback(null, rows);
return;
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7042 次 |
| 最近记录: |