【问题标题】:Of Redux Thunk, Sequelize (Bluebird) - returning promise to action creator "chain"Redux Thunk, Sequelize (Bluebird) - 向动作创建者“链”返回承诺
【发布时间】:2017-11-14 08:40:28
【问题描述】:

与下面引用的一些以前的帖子类似,我正在尝试通过 Thunk 链接 dispatch,但是我的困难在于从 Sequelize 返回。我可以看到 MySQL 查询命中数据库并返回数据,但是该返回没有通过 action-creator 流向后续的.then

我认为这是我尝试使用 Sequelize 的方式 - 我是新手。

非常感谢!

代码:

initDB.js

...
export function sequelizeQry(qryName: string) {
  let query;

  // For later query mapping
  switch (qryName) {
    case 'POSummary':
      query = qry.spPOLedgerCardSummaryALL;
      break;
    default:
      query = qry.spPOLedgerCardSummaryALL;
  }

  return new sequelize.Promise(
    () => sequelize.query(query, { type: sequelize.QueryTypes.RAW })
      .then((response) => {
        console.log('Returning promise: ', response);        //<-- This hits the console with data
        return response;
      })
  );
}

database-actions.js

// @flow
import { fetchingQuery } from '../action-creators/database-creators';

const fetchQuery = (qryName: string) =>
  (dispatch: *) => dispatch(fetchingQuery(qryName));

export default fetchQuery;

database-creators.js

// @flow
// Action Types
import { FETCH_QUERY } from '../constants/ActionTypes';
import { sequelizeQry } from '../utils/initDB';

/** Action-creators */
export function fetchingQuery(qryName: string) {
  const actionType = FETCH_QUERY;

  return (dispatch: *) => {
    dispatch({ type: `${actionType}_PENDING` });
    sequelizeQry(qryName)                          //<-- This gets called
      .then((payload) => dispatch({                //<-- Don't seem to get here
        type: `${actionType}_FULFILLED`,
        payload
      }))
      .catch(err =>
        // Dispatch the error action with error information
        dispatch({
          type: `${actionType}_REJECTED`,
          error: err
        })
      );
  };
}

我检查过的其他一些参考资料:

  1. Redux thunk: return promise from dispatched action
  2. return promise from store after redux thunk dispatch

【问题讨论】:

  • sequelize.Promise 是标准承诺吗?如果是这样,您需要将resolve 作为参数传递给promise executor/handler,然后调用resolve(response) 而不是return response
  • 看起来就是这样,非常感谢。 FLOW 一直在唠叨在 .then 中需要 return,我停止思考......非常感谢!
  • 其实return resolve(response) 让FLOW 很开心……我更新了以下内容。再次感谢。

标签: reactjs redux react-redux sequelize.js redux-thunk


【解决方案1】:

所有功劳归adrice727

这里是未来访问者的代码更改:

...
  return new sequelize.Promise(
    () => sequelize.query(query, { type: sequelize.QueryTypes.RAW })
      .then((response) => {
        console.log('Returning promise: ', response);        //<-- This hits the console with data
        return response;
      })
  );
...

// BECOMES

return new sequelize.Promise(
    (resolve) => sequelize.query(query, { type: sequelize.QueryTypes.RAW })
      .then((response) => {
        console.log('Returning promise: ', response);
        return resolve(response);
      })
  );

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 2018-06-14
    • 2016-11-14
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多