TypeError: undefined is not an object (评估'_order.default.addOrder')

Posted

技术标签:

【中文标题】TypeError: undefined is not an object (评估\'_order.default.addOrder\')【英文标题】:TypeError: undefined is not an object (evaluating'_order.default.addOrder')TypeError: undefined is not an object (评估'_order.default.addOrder') 【发布时间】:2021-01-06 18:53:45 【问题描述】:

我正在尝试将 addOrder 函数从 CartScreen 调用到位于我的操作文件夹中的函数。

问题是每当我按下 OrderNow 按钮时,都会触发 addOrder 函数。但是,我遇到了这样的错误。

CartScreen.js

import React from 'react';
import  View, Text, FlatList, Button, StyleSheet  from 'react-native';
import  useSelector, useDispatch  from 'react-redux';
import * as cartAction from '../../store/actions/cart';
import ordersAction from '../../store/actions/order';
import Colors from '../../constants/Colors';
import CartItem from '../../components/shop/CartItem';

const CartScreen = props => 
  const dispatch = useDispatch();
  const cartTotalAmount = useSelector(state => state.cart.totalAmount);
  const cartItems = useSelector(state => 
    const transformedCartItems = [];
    for (const key in state.cart.items) 
      transformedCartItems.push(
        productId: key,
        productTitle: state.cart.items[key].productTitle,
        productPrice: state.cart.items[key].productPrice,
        quantity: state.cart.items[key].quantity,
        sum: state.cart.items[key].sum,
      );
    
    return transformedCartItems.sort((a, b) =>
      a.productId > b.productId ? 1 : -1,
    );
  );

  console.log('CATRITEM', cartItems);

  return (
    <View style=styles.screen>
      <View style=styles.summary>
        <Text style=styles.summaryText>
          Total:' '
          <Text style=styles.amount>$cartTotalAmount.toFixed(2)</Text>
        </Text>
        <Button
          color='green'
          title="Order Now"
          disabled=cartItems.length === 0
          onPress=() => 
            dispatch(ordersAction.addOrder(cartItems, cartTotalAmount));
          
        />
      </View>
      <FlatList
        data=cartItems
        keyExtractor=item => item.productId
        renderItem=itemData => (
          <CartItem
            quantity=itemData.item.quantity
            title=itemData.item.productTitle
            amount=itemData.item.sum
            onRemove=() => 
              dispatch(cartAction.removeFromCart(itemData.item.productId));
            
          />
        )
      />
    </View>
  );
;

CartScreen.navigationOptions = 
  headerTitle: 'Your Cart',
;

const styles = StyleSheet.create(
  screen: 
    margin: 20,
  ,
  summary: 
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    marginBottom: 20,
    padding: 10,
    shadowColor: 'black',
    shadowOpacity: 0.26,
    shadowOffset:  width: 0, height: 2 ,
    shadowRadius: 8,
    elevation: 5,
    borderRadius: 10,
    backgroundColor: 'white',
  ,
  summaryText: 
    fontSize: 18,
  ,
  amount: 
    color: Colors.primary,
  ,
);

export default CartScreen;

order.js(在行动文件夹中)

export const ADD_ORDER = 'ADD_ORDER';

export const addOrder = (cartItems, totalAmount) => 
  return 
    type: ADD_ORDER,
    orderData: 
      items: cartItems,
      amount: totalAmount,
    ,
  ;
;

【问题讨论】:

【参考方案1】:

以不同的方式使用import 并将函数调用为:

import  addOrder  from '../../store/actions/order';

然后调用如下:

dispatch(addOrder(cartItems, cartTotalAmount));

【讨论】:

@Rooban 刚刚更新了import 部分。你可以这样试试吗? @Rooban 真棒,乐于助人! :)【参考方案2】:

您的导入错误。你像导入 orderAction 一样

import ordersAction from '../../store/actions/order';

你应该做的是

import * as ordersAction from '../../store/actions/order';

之后应该一切正常

【讨论】:

以上是关于TypeError: undefined is not an object (评估'_order.default.addOrder')的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: undefined is not an object (评估'_order.default.addOrder')

为啥当我点击更新按钮时出现错误TypeError: r is undefined?

TypeError: undefined is not a function (in '...styled Components.default.button...')

TypeError: undefined is not a function with React 和 Express

JQUERY DataTable -- TypeError: k is undefined - 使用 MVC 动态形成表

TypeError: undefined is not an object , item is from an array from an API