簡體   English   中英

JavaScript function 返回 0 值

[英]JavaScript function returning 0 value

我正在為節點應用程序編寫路線並調用 function 來計算給定位置的所有訂單的總數。 我能夠控制台記錄 map function (第 12 + 13 行)中的正確值,但是當我返回變量時,我仍然得到 0。 我嘗試將返回移動到 map function 但隨后它為每個值返回 null。

編輯:我立即在 calcTotals function 中聲明了 newTotal 和 newQuantity,並在 map function 中添加了它們。

router.post('/garageOrders', requireLogin, async (req, res) => {
    let {month} = req.body
    try {
        const LocationMonth = await Orders.distinct("location", {"date" : {$regex : ".*Apr.*"}});
        //.find({$contains:{"date": {$regex: `.*${month}.*`}}});
        const calcTotals = (item) => {
            let newTotal = 0;
            let newQuantity = 0;
            Orders.find({ $and: [{"date" : {$regex : ".*Apr.*"}}, {location: item}]})
                .then(resp => {
                    resp.map((order) => {
                        console.log(order.total + newTotal)
                        console.log(order.location.name)
                        console.log('///////////////////////////////////////')
                        newTotal = order.total + newTotal;
                        newQuantity = newQuantity + 1;
                    })
                })
            console.log(newTotal)
            return {
                name: item.name,
                total: newTotal,
                quantity: newQuantity
            }
        }
        const LocationObj = LocationMonth.map((item) =>  calcTotals(item));
        console.log(LocationObj);
        res.send(LocationObj);
    } catch {
        res.status(400).send("No Garages Found");
    }
})

控制台日志

85
Miranova
///////////////////////////////////////
125
75 East Main Street
///////////////////////////////////////
135
SHERATON HOTEL VALET - COLUMBUS
///////////////////////////////////////
270
SHERATON HOTEL VALET - COLUMBUS
///////////////////////////////////////
405
SHERATON HOTEL VALET - COLUMBUS
///////////////////////////////////////
525
SHERATON HOTEL VALET - COLUMBUS
///////////////////////////////////////
560
SHERATON HOTEL VALET - COLUMBUS
///////////////////////////////////////
125
Bicentennial Lot
///////////////////////////////////////
205
#722 South High Garage
///////////////////////////////////////
410
#722 South High Garage
///////////////////////////////////////
190
Crazy Joes Parking Garage
///////////////////////////////////////
120
107 Garage
///////////////////////////////////////
240
107 Garage
///////////////////////////////////////
120
LEVEQUE GARAGE
///////////////////////////////////////
120
City of Columbus Parking Garage
///////////////////////////////////////
240
City of Columbus Parking Garage
///////////////////////////////////////
360
City of Columbus Parking Garage
///////////////////////////////////////
480
City of Columbus Parking Garage
///////////////////////////////////////

重新發送

[
    {
        "name": "Miranova",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "75 East Main Street",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "SHERATON HOTEL VALET - COLUMBUS",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "Bicentennial Lot",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "#722 South High Garage",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "107 Garage",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "City of Columbus Parking Garage",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "Crazy Joes Parking Garage",
        "total": 0,
        "quantity": 0
    },
    {
        "name": "LEVEQUE GARAGE",
        "total": 0,
        "quantity": 0
    }
]

您的 map function 是異步代碼的一部分,並且返回在該異步代碼之外。 function 在 map 可能獲取數據之前返回。 嘗試這樣的事情

router.post('/garageOrders', requireLogin, async (req, res) => {
  let {month} = req.body
  try {
      const LocationMonth = await Orders.distinct("location", {"date" : {$regex : ".*Apr.*"}});
      //.find({$contains:{"date": {$regex: `.*${month}.*`}}});
      const calcTotals = async (item) => {
          let newTotal = 0;
          let newQuantity = 0;
          let ordersRes = await Orders.find({ $and: [{"date" : {$regex : ".*Apr.*"}}, {location: item}]})
          ordersRes.forEach((order) => {
                      console.log(order.total + newTotal)
                      console.log(order.location.name)
                      console.log('///////////////////////////////////////')
                      newTotal = order.total + newTotal;
                      newQuantity = newQuantity + 1;
                  })
          console.log(newTotal)
          return {
              name: item.name,
              total: newTotal,
              quantity: newQuantity
          }
      }
      const LocationObj = await LocationMonth.map(async (item) =>  await calcTotals(item));
      console.log(LocationObj);
      res.send(LocationObj);
  } catch {
      res.status(400).send("No Garages Found");
  }
})

不幸的是,我無法對其進行測試以確保其正常工作,但使用 async await 將有助於確保數據在映射並返回之前返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM