依靠另一个查询结果的nodejs mysql查询,然后将第二个查询结果循环插入第一个

时间:2019-07-03 18:21:11

标签: mysql node.js

我需要运行第一个查询,我从中获取拍卖列表,我需要运行第二个查询,对于每次拍卖,我都获得该拍卖的最低出价。 得到结果后,我需要将最低出价推送到我从for循环中的第一个查询(results1)中获取的Auctions json。 当我控制台日志结果时,我得到一个包含9个对象的数组(应该为3个),并且最低出价属性仅存在于其中3个对象上,这可能是更好的方法。 将结果设为最低出价后,返回console.log结果: https://imgur.com/a/eiYcycZ

router.get('/get-live-auctions', auth, (req, res) => {
  try {

    const userID = req.userData.userID;
    db.query(`SELECT auctions.UID,auctions.OriginCompany,auctions.DestinationCompany,auctions.OriginAddress,auctions.DestinationAddress,auctions.PickupDate,auctions.TotalWeight,auctions.StartDate,auctions.BidEndDate,auctions.AuctionEndDate,auctions.AuctionState,auctions.AuctionSerialNumber
    From auctions
    WHERE UserId='${userID}' AND AuctionState = 2 OR AuctionState = 3 OR AuctionState = 4`, (
      err, results, fields) => {
        for (let i = 0; i < results.length; i++) {
          let auctionsIDS = results[i].UID;
          db.query(`SELECT MIN(TotalPrice) AS lowestBid
          FROM bids
          Where AuctionID = '${auctionsIDS}'
          `, (err2, results2, fields2) => {
              let lowestBid = results2[0].lowestBid;
              results.lowestBid = lowestBid;
              console.log(results);
              if (err2) return res.send(error);
            }
          )
        }
        res.status(200).json(results);
      });

  } catch (error) {
    return res.status(500).send("Server error");
  }
})

如何解决此问题?

0 个答案:

没有答案