我如何输出多个值(折扣)?

问题描述 投票:0回答:1

我正在尝试了解如何返回多个值。现在,我的代码被设置为针对具有完全一个兑换代码的任何代码返回单个对象(redeemDisc)。我的问题是,如果一个代码有多个赎回代码,它只会返回“多次折扣”。我试图了解如何将'redeemDisc'存储在数组中,以便在有多个折扣的情况下,我可以只退还所有折扣]

redeemCode({ userUuid, code }) {
let error;
let locationId;
return this.models.BusinessLocation.findOne({
  where: {
    code: code,
    deleted: false
  }
}).then(loc => {

  if (loc === null)
  {
    error = 'Invalid Code';
    return;
  }
  locationId = loc.id;
  return this.models.OfferLocation.findAndCountAll({
    where: {
      locationId: loc.id
    }
  })
}).then(async offerLocs => {

    if (offerLocs.length === 0)
    {
      return "Zero Discounts";
    }
    let count = 0;
    let offer;
    let i;
    for ( i = 0; i < offerLocs.rows.length;i++)
    {
        let offerLoc = offerLocs.rows[i];
        offer = await this.models.Discount.findOne({
          where: {
            id: offerLoc.offerId
          }
        });

        if (!offer.active || offer.deleted)
        {
          continue;
        }

        count++;
    }

    console.log('redeeemCode -service (offer count)', count) ;       
    console.log('redeeemCode -service (offer )', JSON.stringify(offer)) ;       

    if (count > 1)
    {
      return 'multiple discounts';
    }
    if (count === 0)
    {
      return 'zero discounts';
    }

    // have a single offer - claim

    return this.redeemDisc(
      { discountId: offer.id , 
        userUuid: userUuid, 
        code: code, 
        locationId: locationId
      }).then(redeemed => {
        return redeemed;
        }
        )


})

}

对于一次打折,它以]的形式返回JSON>

"redeemed": {
    "createdAt": "2019-10-27T23:54:14.031Z",
    "updatedAt": "2019-10-27T23:54:14.031Z",
    "id": "143751f0-f915-11e9-8107-658322eb948c",
    "UserUuid": "b136ccd9-0783-482d-956b-7082286051b3",
    "DiscountId": "97cfdf90-3ee0-11e9-b720-c1f2606cbbd2",
    "BusinessId": "978a7220-3ee0-11e9-b720-c1f2606cbbd2",
    "locationId": "97cf1c40-3ee0-11e9-b720-c1f2606cbbd2"
}

我需要了解如何返回与给定代码相关的所有折扣,因此预期的输出将是

兑换:{...},兑换:{...},...

我正在尝试了解如何返回多个值。现在,我的代码被设置为针对具有完全一个兑换代码的任何代码返回单个对象(redeemDisc)。我的问题是,如果...

javascript node.js reactjs
1个回答
0
投票

由于您有多个与一个代码相关的要约,因此必须使用要约数组而不是变量,并且应将要约的每个细节存储在该要约数组中。

© www.soinside.com 2019 - 2024. All rights reserved.