Tag: redux thunk

Redux – 通过键在状态数组中查找对象并更新其另一个键

我正在使用后端的NodeJS和前端的React + Redux的CRUD应用程序。 这是一个如何运作的快速模式: 添加一个项目 用户inputpost的标题 然后将标题发送到在NodeJS中获取POST路由并通过主体发送标题的调度 在NodeJS路由中,我添加了一个新的项目集合与该标题 BACKEND DONE,新的post在服务器中 我在步骤2中添加.then()调度function,其中我派遣一个type: 'ADD_POST'并post: post (我从res.json({post: result from database}) ) 在我的reducer中,我设置了一个CASE 'ADD_POST': return action.post 完成FRONTEND后,用户现在可以看到同一个post,而不需要刷新 我想使用相同的逻辑来更新特定post的喜好 。 这是我迄今为止所做的: 点击一个post上的button会触发一个获取NodeJS PUT路由的调度 NodeJS路由查找使用ID的文章,并添加1到喜欢该post的旧值 BACKEND DONE,现在在服务器上有1个以上 我再次添加到获取NodeJS路由的调度中,在该调度中,我分配了一个type 'ADD_LIKE'的操作,并更新了更新的post ID 在reducer中,我设置了一个CASE 'ADD_LIKE':它使用下面代码片段中的代码遍历整个状态。 这是这个片段: state.map(item => { if(item.id === action.postid) { //i find the specific item, but now im stuck on how […]

Redux-Thunk“操作必须是普通对象。 使用自定义中间件进行asynchronous操作。“

使用Thunk中间件创build我的商店 import { createStore, applyMiddleware, compose } from 'redux'; import thunk from 'redux-thunk'; const store = createStore( reducer, initialState, applyMiddleware(thunk) ); 并创造我的行动,呼吁一个承诺 export function getArticle(url) { return function (dispatch) { fetchArticle(url).then( article => dispatch(setArticle(article)), ); }; } function fetchArticle(url) { return new Promise((resolve, reject) => { request .get(url) .end( (err, res) => { if (err […]

ReactJS + Redux:即使有正确的API请求,为什么MongoDB不把数据保存到数据库?

我的ReactJS + Redux项目中有一个MongoDB / Webpack / NodeJS Express。 我正在使用redux中的动作创build者进行API调用,然后到达API服务器并返回成功状态,但是数据永远不会被保存,甚至在terminalmongo -> dbs也不会创build数据库,并且不会显示我把它命名为。 可能是什么问题? 我错过了什么吗? 任何指导或见解将不胜感激。 谢谢 这是我的API设置: import axios from 'axios'; import { browserHistory } from 'react-router'; import cookie from 'react-cookie'; import { AUTH_USER, AUTH_ERROR } from './types'; const API_URL = 'http://localhost:3000/api'; export function errorHandler(dispatch, error, type) { let errorMessage = (error.data.error) ? error.data.error : error.data; […]