Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获错误:操作必须是普通对象_Javascript_Reactjs_Redux_Axios_Redux Thunk - Fatal编程技术网

Javascript 未捕获错误:操作必须是普通对象

Javascript 未捕获错误:操作必须是普通对象,javascript,reactjs,redux,axios,redux-thunk,Javascript,Reactjs,Redux,Axios,Redux Thunk,我已经用这个标题把所有的问题都问了一遍,我找不到一个解决方案来防止这个错误。到目前为止,我一直在努力确保我不会混淆新旧的redux api,我正在使用babel进行传输,因此不会应用任何类型脚本解决方案,我确保在相关操作中返回一个函数,我已注释掉我的导入或thunk,以确保它中断,导入后我已注销thunk,并获得一个函数,我已经更新了devtools扩展,它是从去润滑版本开始的。没有成功。非常感谢您的帮助。有关守则如下: 商店: const redux = require('redux'); c

我已经用这个标题把所有的问题都问了一遍,我找不到一个解决方案来防止这个错误。到目前为止,我一直在努力确保我不会混淆新旧的redux api,我正在使用babel进行传输,因此不会应用任何类型脚本解决方案,我确保在相关操作中返回一个函数,我已注释掉我的导入或thunk,以确保它中断,导入后我已注销thunk,并获得一个函数,我已经更新了devtools扩展,它是从去润滑版本开始的。没有成功。非常感谢您的帮助。有关守则如下:

商店:

const redux = require('redux');
const {combineReducers, createStore, compose, applyMiddleware} = require('redux');
import {default as thunk} from 'redux-thunk';

const {nameReducer, hobbyReducer, movieReducer, mapReducer} = require('./../reducers/index');

export const configure = () => {

    const reducer = combineReducers({
        name: nameReducer,
        hobbies: hobbyReducer,
        movies: movieReducer,
        map: mapReducer
    });

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

    const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk)));   

    return store;
};
行动:

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url};

export let fetchLocation = () => (dispatch, getState) => {
    dispatch(startLocationFetch());

    axios.get('http://ipinfo.io').then(function(res) {
        let loc = res.data.loc;
        let baseURL = 'http://maps.google.com?q=';

        dispatch(completeLocationFetch(baseURL + loc));
    });
};
发送操作的代码:

console.log('starting redux example');

const actions = require('./actions/index');
const store = require('./store/configureStore').configure();

let unsubscribe = store.subscribe(() => {
    let state = store.getState();

    if(state.map.isFetching){
        document.getElementById('app').innerHTML = 'Loading...';
    } else if(state.map.url){
        document.getElementById('app').innerHTML = '<a target=_blank href = "' + state.map.url + '">Location</a>';
    }
});



store.dispatch(actions.fetchLocation());
console.log('starting redux example');
常量操作=要求('./操作/索引');
const store=require('./store/configureStore').configure();
让我们取消订阅=存储。订阅(()=>{
let state=store.getState();
if(state.map.isFetching){
document.getElementById('app').innerHTML='Loading…';
}else if(state.map.url){
document.getElementById('app')。innerHTML='';
}
});
store.dispatch(actions.fetchLocation());
我现在正在学习React/Redux(这是一门课程),所以我真的可能遗漏了一些明显的东西。如果我遗漏了一些相关的内容,请告诉我。谢谢

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};
不是返回对象,而是应该导致语法错误

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url}
要从箭头函数直接返回对象,需要设置括号,否则将被解释为块:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

编辑:感谢Nicholas指出这确实不是语法错误

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url}
不是返回对象,而是应该导致语法错误

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url}
要从箭头函数直接返回对象,需要设置括号,否则将被解释为块:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

编辑:感谢Nicholas指出这确实不是语法错误

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url}
我认为这些线路就是问题所在。函数的简短语法效果很好,但对于返回对象,应使用括号将其括起来,如下所示:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

export let completeLocationFetch = (url) => ({type: 'COMPLETE_LOCATION_FETCH', url})
因此,transpiler知道下一步是必须返回的单个参数,而不是函数体

我认为这些线路就是问题所在。函数的简短语法效果很好,但对于返回对象,应使用括号将其括起来,如下所示:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

export let completeLocationFetch = (url) => ({type: 'COMPLETE_LOCATION_FETCH', url})

因此,transpiler知道接下来必须返回的是单个参数,而不是函数体。

应该会导致语法错误。
值得注意的是,这是有效的语法<代码>类型:被解释为标签语句,然后后跟字符串表达式。多么阴险……是的!非常感谢你。我很高兴它是这样的,而不是redux/thunk部分。
应该会导致语法错误。
值得注意的是,它是有效的语法<代码>类型:被解释为标签语句,然后后跟字符串表达式。多么阴险……是的!非常感谢你。我很高兴它是这样的,而不是redux/thunk部分使用
从“redux thunk”导入thunk
在导入时不使用大括号,默认导出将保存在您在其中选择的任何名称上(在本例中为
thunk
)。@cfraser这很有趣。在文档中,它说“如果您在CommonJS环境中使用Redux Thunk 2.x,请不要忘记在导入中添加.default”,因为我使用的是2.1,我假设需要导入或特别需要.default,但我只是尝试了您所说的,没有错误。我想知道为什么它特别声明了它,但它不是必需的。这是因为它不是CommonJS,而是ES5。CommonJS将使用
var x=require('something')。默认值为
。当然可以。非常感谢。您还可以将{code>import{default as thunk}替换为'redux thunk'使用
从“redux thunk”导入thunk
在导入时不使用大括号,默认导出将保存在您在其中选择的任何名称上(在本例中为
thunk
)。@cfraser这很有趣。在文档中,它说“如果您在CommonJS环境中使用Redux Thunk 2.x,请不要忘记在导入中添加.default”,因为我使用的是2.1,我假设需要导入或特别需要.default,但我只是尝试了您所说的,没有错误。我想知道为什么它特别声明了它,但它不是必需的。这是因为它不是CommonJS,而是ES5。CommonJS将使用
var x=require('something')。默认值为
。当然可以。非常感谢。