javascript - 如何解决未捕获的类型错误: Illegal invocation function for uuid() in javascript

标签 javascript reactjs

我正在创建用于在 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 .

关于javascript - 如何解决未捕获的类型错误: Illegal invocation function for uuid() in javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205926/

相关文章:

javascript - 使用 snap.svg IE 和 EDGE 网络浏览器快速悬停时使用 animate 进行放大/缩小

javascript - 从 jQuery onChange 使用 Vue 更新数据值

javascript - Electron:无法在BrowserWindow中隐藏程序图标

javascript - 如何克服来自两个 CSS 框架的相似名称 CSS 类的重叠?

javascript - 在 anchor 元素上 react 传递事件和参数

javascript - 在 Ember 中渲染到导出时获取正确的数据模型

javascript - object is not extensible 错误在vuejs

reactjs - 启用 Debug模式时,不会在 Android 上调用 React Native <TouchableOpacity> 回调

javascript - React.js 未捕获类型错误

reactjs - react 应用程序未使用 firebase 正确部署