【问题标题】:How do i return a text statement as well as a function output in the same return statement?如何在同一个返回语句中返回文本语句以及函数输出?
【发布时间】:2019-11-02 06:49:06
【问题描述】:

我试图了解如何在if (count > 1) 内返回“多重折扣”以及 this.redeemDisc

`if (count > 1) {
    //return 'multiple discounts';
    return this.redeemDisc(
      {
        //BusinessId: BusinessId,
        discountId: offer.id,
        userUuid: userUuid,
        code: code,
        locationId: locationId
      }).then(redeemed => {
        return redeemed;
      }
      )
  }`

感谢任何帮助!

【问题讨论】:

  • 你能给我this.redeemDisc的回复吗?
  • 邮递员回复是"redeemed": { "createdAt": "2019-11-01T22:23:17.168Z", "updatedAt": "2019-11-01T22:23:17.168Z", "id": "33ba42f0-fcf6-11e9-824d-ede07167778c", "UserUuid": "b136ccd9-0783-482d-956b-7082286051b3", "DiscountId": "91c0a250-fc1e-11e9-ac4a-e93a04a07c14", "BusinessId": "60b7c080-b88f-11e9-932c-e5ea93cc8e76", "locationId": "66743120-fc1e-11e9-ac4a-e93a04a07c14"
  • 我需要更多上下文...redeemDisc 似乎只返回一张光盘的数据...您有光盘列表吗?
  • 什么是“多重折扣”?就像字符串一样?
  • redeemDisc 函数太长,无法在评论中发布。我有一个在邮递员响应中显示的折扣列表,businessId 在特定位置保存多个折扣的信息。我只需要找到一种方法,以便当if (count > 1) 被触发时,它会输出我刚刚列出的邮递员响应,这是目前所做的,并且我还需要包含“多重折扣”一词。以我目前的知识,我只知道如何做其中之一,但不知道两者都做

标签: javascript node.js reactjs sequelize.js


【解决方案1】:

这将返回一个数组,其中第一个解析是您的字符串,第二个是请求的结果。您将同时获得两者。

    if (count > 1) {
    return Promise.all([Promise.resolve('multiple discounts'), this.redeemDisc(
        {
            //BusinessId: BusinessId,
            discountId: offer.id,
            userUuid: userUuid,
            code: code,
            locationId: locationId
        });
    )])
  }

【讨论】:

  • 感谢您的回复,这非常有效,我需要审查承诺,我什至没有考虑过这个。
  • @mjm 太好了!您介意将此标记为正确答案吗?谢谢!
【解决方案2】:

你不能返回一个字符串和一个对象,但你可以用你的消息返回对象

return { ...redeemed, message: 'multiple discounts' }

【讨论】:

  • 感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 2021-09-27
  • 2011-06-06
相关资源
最近更新 更多