Node.js MySQL的递归查询问题

Node.js MySQL的递归查询问题,node.js,Node.js,我正在构建一个递归查询: app.get('/showposts', function(res, req) { getPosts(id){ connection.query("SELECT * FROM posts WHERE someId = 2", (err, rows) => { allIds = []; for(var i = 0; i < rows.length; i++){ allIds.push(row

我正在构建一个递归查询:

app.get('/showposts', function(res, req) {

  getPosts(id){

    connection.query("SELECT * FROM posts WHERE someId = 2", (err, rows) => {

      allIds = [];

        for(var i = 0; i < rows.length; i++){
          allIds.push(rows[i].id_post);
          getPosts(rows[i].id_post)     // <= calling the function again.
        }

        console.log("The IDs : " + allIds )  
        // res.send fails stating response was already sent

        });
    };

    getPosts(1)  // <= initial call to the function.
});

我做错了什么?请提供帮助。

您正在使用异步函数,因为它是同步的

看看
Promise
async/await
模式


编辑:如你所问-没有尝试过,这是一个开始

  getPosts(id) {
    return new Promise((resolve, reject) => {
      connection.query(
        `SELECT * FROM posts WHERE someId = ${id}`,
        (err, rows) => {
          // Nothing more to do, we can leave
          if (!rows.length) return resolve([]);

          // Call the getPosts on the new ids we just read
          return Promise.all(rows.map(x => getPosts(x.id_post)))
            .then((rets) => {
              // We return the ids returned from the recursive 
              // getPosts and from the actual getPosts

              // We want resolve to return something like
              // [ id1, id2, id3, ... ]
              resolve([
                // rets here worth something like :
                //  
                //  [  [ id1, id2, id3 ...], [ id4, id5, id6 ...], ... ]
                //  
                // we gonna turn it into [ id1, id2, id3, id4, id5, id6, ... ]
                // 
                ...rets.reduce((tmp, x) => [...x, ...tmp], []),
                ...rows.map(x => x.id_post),
              ]);
            })
            .catch(reject);
      });
    });
  };

    // call the func
    getPosts(1)
     .then(ids => console.log(ids))
     .catch(err => console.log(String(err)));

您正在使用异步函数,因为它是同步的

看看
Promise
async/await
模式


编辑:如你所问-没有尝试过,这是一个开始

  getPosts(id) {
    return new Promise((resolve, reject) => {
      connection.query(
        `SELECT * FROM posts WHERE someId = ${id}`,
        (err, rows) => {
          // Nothing more to do, we can leave
          if (!rows.length) return resolve([]);

          // Call the getPosts on the new ids we just read
          return Promise.all(rows.map(x => getPosts(x.id_post)))
            .then((rets) => {
              // We return the ids returned from the recursive 
              // getPosts and from the actual getPosts

              // We want resolve to return something like
              // [ id1, id2, id3, ... ]
              resolve([
                // rets here worth something like :
                //  
                //  [  [ id1, id2, id3 ...], [ id4, id5, id6 ...], ... ]
                //  
                // we gonna turn it into [ id1, id2, id3, id4, id5, id6, ... ]
                // 
                ...rets.reduce((tmp, x) => [...x, ...tmp], []),
                ...rows.map(x => x.id_post),
              ]);
            })
            .catch(reject);
      });
    });
  };

    // call the func
    getPosts(1)
     .then(ids => console.log(ids))
     .catch(err => console.log(String(err)));

谢谢您能否帮助我了解如何在这种情况下实现
。然后
?这是正确的方法吗?我只需要一个
伪代码
,我可以处理它。再次感谢。哇。谢谢这很有效。我也了解了ES6承诺。多练习。我真的很感激,伙计。谢谢你!谢谢您能否帮助我了解如何在这种情况下实现
。然后
?这是正确的方法吗?我只需要一个
伪代码
,我可以处理它。再次感谢。哇。谢谢这很有效。我也了解了ES6承诺。多练习。我真的很感激,伙计。谢谢你!