gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:19:57 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com