简体   繁体   English

如何将子查询添加到带有轴的查询结果中

[英]How to add a subquery to the results of a query with axis

I am querying an objects table based no an id .我正在查询一个没有idobjects表。 Then I want to query the same table with the id of my result and add it result object as a new property.然后我想用我的结果的 id 查询同一个表,并将它的结果对象添加为新属性。 Hope, that makes sense:希望,这是有道理的:

app.get(`/details`, (req, res) => {
  const { id } = req.query;
  connection.query(
    `SELECT
      objects.*
    FROM objects
    WHERE id = ${id}
    LIMIT 1`,
    (err, results) => {
      if (err) {
        return res.send(err);
      } else {
        let result = results[0];
        result.relationships = [];
        connection.query(
          `SELECT
            objects.*
          FROM objects
          WHERE relationship = ${result.id}`,
          (err, rel_results) => {
            if (err) {
              return res.send(err);
            } else {
              rel_results.forEach((el) => {
                result.relationships.push(el);
              });
              // This log looks fine. result.relationhips is an array of objects.
              console.log(result);
            }
          }
        );
        // Here, the result has an empty array for result.relationhips
        return res.json(result);
      }
    }
  );
});

When you look at the comments, why is the result correct in the log but not in the return?查看评论时,为什么结果在日志中是正确的,而在返回中却不是?

You need to place the return statement within the nested callback, this is main because of early return:您需要将 return 语句放在嵌套回调中,这是主要因为提前返回:

result.relationships = [];
        connection.query(
          `SELECT
            objects.*
          FROM objects
          WHERE relationship = ${result.id}`,
          (err, rel_results) => {
            if (err) {
              return res.send(err);
            } else {
              rel_results.forEach((el) => {
                result.relationships.push(el);
              });
              // This log looks fine. result.relationhips is an array of objects.
              console.log(result);
              return res.json(result);
            }
          }
        );
       

You dont acutally have to build a promise.您实际上不必建立承诺。 You can simply send your results when they are available within your second query callback:当结果在第二个查询回调中可用时,您可以简单地发送结果:

(err, results) => {
  if (err) {
    return res.send(err);
  } else {
    let result = results[0];
    result.relationships = [];
    connection.query(
      `SELECT
        objects.*
      FROM objects
      WHERE relationship = ${result.id}`,
      (err, rel_results) => {
        if (err) {
          return res.send(err);
        } else {
          rel_results.forEach((el) => {
            result.relationships.push(el);
          });
          // This log looks fine. result.relationhips is an array of objects.
          console.log(result);

          res.json(result);
        }
      }
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM