首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React :从同一个文件调用异步方法

React :从同一个文件调用异步方法
EN

Stack Overflow用户
提问于 2021-07-14 06:17:10
回答 3查看 88关注 0票数 0

我试图调用存在于同一个文件中的removeCouponCode方法,但是执行说它没有定义,我不确定这里缺少什么。有什么想法吗?下面是我正在编辑的文件&试图进行更改。不知道缺了什么。

请看一看,让我知道缺少了什么

代码语言:javascript
复制
import React, { useState, useEffect, useMemo } from 'react';
import SessionContext from 'react-storefront/session/SessionContext';
import PropTypes from 'prop-types';
import fetch from 'react-storefront/fetch';
import get from 'lodash/get';
import { EventTracking, AnalyticsErrors } from '../analytics/Events';
import Cookies from 'js-cookie';
import constants from '../constants/configs';
import errorHandler from '../constants/apiHelpers/errorHandler';
import Helper from '../constants/helper';
import configs from '../constants/configs';
const { session, actions } = useContext(SessionContext);
const initialState = {
  signedIn: false,
  cart: {
    items: [],
    shipping_methods: [],
    shippingCountry: null,
    shippingMethodCode: null,
    freeShipingMethod: null,
    freeShippingSelected: false
  },
  customer: {
    store_credit: {},
    wishlist: { items_count: 0, items: [] },
    offline: true
  },
  errMsg: ''
};
const initialStatusState = {
  cartLoadingStatus: false,
  setShippingMethodStatus: false
};

    async redemptionCode({ couponCode, ...otherParams }) {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/redemptionCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ couponCode, ...otherParams })
          });

          const result = await response.json();

          if (response.ok) {
            if(get(result, 'cart.prices.discounts', {}) === null){
              removeCouponCode(...otherParams); //says undefined removeCouponCode here
            }
            
          }
        },
        async removeCouponCode({ ...otherParams }) {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/removeCouponCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ ...otherParams })
          });
        },
EN

回答 3

Stack Overflow用户

发布于 2021-07-14 06:26:26

您没有引用您的函数,也没有引用您的函数,或者您可以按如下方式修改代码,将其声明为一个函数,

代码语言:javascript
复制
async function removeCouponCode({ ...otherParams }) {
    let tempSession = { ...session };
    const response = await fetch('/api/cart/removeCouponCode', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ ...otherParams })
    });
}

async function redemptionCode({ couponCode, ...otherParams }) {
    let tempSession = { ...session };
    const response = await fetch('/api/cart/redemptionCode', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ couponCode, ...otherParams })
    });

    const result = await response.json();

    if (response.ok) {
        if(get(result, 'cart.prices.discounts', {}) === null){
            await removeCouponCode(...otherParams);
        }

    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-07-14 07:16:46

您没有将它们声明为function。所以你可以用这种方式来定义它们

代码语言:javascript
复制
async function redemptionCode({ couponCode, ...otherParams }) {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/redemptionCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ couponCode, ...otherParams })
          });

          const result = await response.json();

          if (response.ok) {
            if(get(result, 'cart.prices.discounts', {}) === null){
              removeCouponCode(...otherParams); //says undefined removeCouponCode here
            }
            
          }
        },
        async function removeCouponCode({ ...otherParams }) {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/removeCouponCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ ...otherParams })
          });
        },

或者是这边

代码语言:javascript
复制
const redemptionCode = async ({ couponCode, ...otherParams }) => {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/redemptionCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ couponCode, ...otherParams })
          });

          const result = await response.json();

          if (response.ok) {
            if(get(result, 'cart.prices.discounts', {}) === null){
              removeCouponCode(...otherParams); //says undefined removeCouponCode here
            }
            
          }
        },
        const removeCouponCode = async ({ ...otherParams }) => {
          let tempSession = { ...session };
          const response = await fetch('/api/cart/removeCouponCode', {
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ ...otherParams })
          });
        },

两样都能用!

票数 0
EN

Stack Overflow用户

发布于 2021-07-14 07:21:47

似乎您在调用它之后定义了这个函数。我认为您应该首先定义它,然后调用it..and,您可以使用箭头函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68372958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档