如何解决未捕获的类型错误:JavaScript中uuid()的非法调用函数



我正在创建用于在 Redux 存储中添加数据的存储,因为我使用了 uuid(( 函数,但它会引发运行时异常"未捕获的类型错误:非法调用"。

import {createStore,combineReducers} from 'redux';
import uuid from 'uuid';
//Add Expense
const addExpense = (
{description='',note='',amount=0, createdAt=0} ={}) =>({
type:'ADD_DETAIL',
expense: {
   id:uuid(),
    description,
    note,
    amount,
    createdAt
}
})
//Expenses Reducers
const detailDefaultState  = [];
const detailReducers = (state=detailDefaultState,action) =>{
switch(action.type){
    case  'ADD_DETAIL':
    return state.concat(action.expense)
    default:
        return state;
}
};
//Store Creation
const store = createStore(
combineReducers({
    expenses:detailReducers,       
})
);
store.subscribe(()=>{
console.log(store.getState());
})
store.dispatch(addExpense({description:'Rent',amount:100}));

如果我从代码中删除 uuid(( 函数,它的运行正确。uuid(( 的版本是 3.2.0。

您没有正确导入uuid。您需要指定要使用的uuid类型,例如

// v4 - random uuid
import uuid from 'uuid/v4';
uuid(); // ⇨ will produce something like '45db52e1-f95c-4b5f-99a2-8b8d978c99b4'

您可以在此处阅读有关类型的更多信息:https://github.com/kelektiv/node-uuid。

最新更新