gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:40:43 26 4
gpt4 key购买 nike

我试图了解如何返回多个值。现在,我的代码设置为为任何具有一个兑换代码的代码返回单个对象(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"
}

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

兑换:{ ... },赎回:{...},...

最佳答案

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

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

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

count++;
}

现在您可以像这样存储并返回折扣数组。

var discount = []
for(let j =0; j<offer.length; ++j) {
let redeem = await this.redeemDisc(
{ discountId: offer.id ,
userUuid: userUuid,
code: code,
locationId: locationId
})
discount.push({"redeemed": redeem})

}

return discount;

关于javascript - 如何输出多个值(折扣)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58596436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com