【问题标题】:Redux 'dispatch' function isn't called未调用 Redux 'dispatch' 函数
【发布时间】:2018-08-13 12:38:18
【问题描述】:
import firebase from "firebase";
import "firebase/firestore";
import moment from "moment";
import firebaseApp from "../firestoreConfig";
import { config } from "../../App";
import FETCH_PRICES from "./types";

const firestore = firebaseApp.firestore();

export const getPrices = () => {
  return dispatch => {
    let prices;
    firestore
      .collection("settings-daily-market")
      .orderBy("MarketDate", "desc")
      .limit(1)
      .get()
      .then(snapshot => {
        snapshot.forEach(doc => {
          const Pd = doc.data().PdMarket;
          const Pt = doc.data().PtMarket;
          const Rh = doc.data().RhMarket;
          prices = { Pd, Pt, Rh };
          console.log(prices);
        });
        dispatch({ type: FETCH_PRICES, payload: prices });
        console.log("dispatched");
      });
  };
};

带有价格的console.log 运行,但第二个console.log('dispatched') 从不运行,并且我的“FETCH_PRICES”类型的reducer 代码没有运行。相反,我得到一个错误:

"YellowBox.js:82 可能的未处理承诺拒绝 (id: 0): 错误:操作可能没有未定义的“类型”属性。你有没有拼错一个常数?”

为什么 dispatch() 不起作用?

【问题讨论】:

  • 看起来FETCH_PRICES 没有类型属性。你能发帖./types吗?
  • 调用dispatch之前FETCH_PRICES的值是多少?
  • 谢谢大家!我错误地导入了 FETCH_PRICES。

标签: javascript reactjs redux redux-thunk


【解决方案1】:

我想你想要import { FETCH_PRICES } from "./types";,因为我无法想象你将 FETCH_PRICES 导出为默认值。

【讨论】:

  • facepalm 非常感谢。这就是问题所在。
猜你喜欢
  • 2021-11-11
  • 2017-10-31
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2017-04-30
  • 2021-04-26
  • 2017-06-09
相关资源
最近更新 更多