javascript - Express.js (Node.js) 上的异步 SqLite 模块

标签 javascript node.js sqlite express

我正在尝试向数据库发出同步请求并将数据传递给表达。这是代码

app.get('/', (req, res) => {
  let db = new sqlite3.Database('./Problems.db');
  let sql = 'SELECT * FROM Problems ORDER BY problem_id';
  let data;
  db.all(sql, [], (err, rows, res) => {

    data = DBFetchingAllData(rows, db);
  });
  res.render('pages/index', { data });
});

由于db.all()是异步函数,res.renders捕获undefined,这是我的问题之一。第二个问题是在函数DBFetchingAllData中,我认为它返回rows,但实际上什么也没返回。如果有人帮助我使 DBFetchingAllData 正确返回行并使 db.all() 同步,我将非常感激。

function DBFetchingAllData(rows, db) {

    rows.forEach((row, index) => {
    // loop over the problem_questions_id
    // Array with answers
    row.answer = [];
    let row_with_id = _.split(row.problem_questions_id, ',');

    row_with_id.forEach((id) => {
      let sql = `SELECT * FROM QuestionsAndAnswers WHERE id = ?`;
      // use db.all not db.get to fetch an array of answers
      // this call is asynchronous so we need to check if we are done inside the callback
      db.get(sql, [id], (err, answer) => {
        // "answers" is an array here
        row.answer.push(answer);
        // if the index is one less than the length it's the last
        if (index === rows.length-1) {
          // we're done!
          return rows;
        }
      });
    });
  });
}

最佳答案

第一个问题的解决方案:

只需在所有回调函数中调用res.render即可

app.get('/', (req, res) => {
  let db = new sqlite3.Database('./Problems.db');
  let sql = 'SELECT * FROM Problems ORDER BY problem_id';
  let data;
  db.all(sql, [], (err, rows, res) => {

    data = DBFetchingAllData(rows, db);
    res.render('pages/index', { data });
  });

});

第二个解决方案: 您忘记在函数末尾返回 rows :

      function DBFetchingAllData(rows, db) {
         return Promise((resolve)=>{

            rows.forEach((row, index) => {
            // loop over the problem_questions_id
            // Array with answers
            row.answer = [];
            let row_with_id = _.split(row.problem_questions_id, ',');

            row_with_id.forEach((id) => {
              let sql = `SELECT * FROM QuestionsAndAnswers WHERE id = ?`;
              // use db.all not db.get to fetch an array of answers
              // this call is asynchronous so we need to check if we are done inside the callback
              (function(row,index){
                 db.get(sql, [id], (err, answer) => {
                   // "answers" is an array here
                   row.answer.push(answer);
                   // if the index is one less than the length it's the last
                   if (index === rows.length-1) {
                  // we're done!
                      resolve(rows)
                   }
                 });
              })(row,index)
            });

          });
        });
    }

关于javascript - Express.js (Node.js) 上的异步 SqLite 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47242865/

相关文章:

android - 如何使 SQLite 选择查询更快

Android-SQLite。一对多关系

javascript - 控制或分割数组中字符串中 `:`前后的目标和值

node.js - 使用 Alexa 计数

python - 将值从下拉菜单传递到 Flask 模板

javascript - Node.js 不等待数据

javascript - 在 Node.JS 中,我如何使用 Google Sheets API "shift"增加整个值范围?

javascript - 如何避免可观察的 Angular 延迟或确保仅当可观察准备好时才调用我的函数

javascript - 动态 html/php

javascript - 嵌套的 jquery 选择器触发父子特定事件