在redux中删除请求

时间:2018-04-26 02:29:15

标签: node.js reactjs react-redux

我正在使用Node.js API编写React Redux CRUD应用程序。我正在努力解决 DELETE 问题。 我收到了成功的删除邮件,但我的数据库中没有任何更改。 Successful Message in Console

我只是想知道它为什么不删除任何数据?

user.reducer:

import { userConstants } from '../_constants';

export function users(state = {}, action) {

  switch (action.type) {

    case userConstants.GETALL_REQUEST:
      return {
        loading: true
      };
    case userConstants.GETALL_SUCCESS:
      return {
        items: action.users
      };
    case userConstants.GETALL_FAILURE:
      return { 
        error: action.error
      };
    case userConstants.DELETE_REQUEST:
      // add 'deleting:true' property to user being deleted
      return {
        ...state,
        items: state.items.map(user =>
          user.id === action.id
            ? { ...user, deleting: true } 
            : user
        )
      };
    case userConstants.DELETE_SUCCESS:
      // remove deleted user from state
      return {
        items: state.items.filter(user => user.id !== action.id)
      };
    case userConstants.DELETE_FAILURE:
      // remove 'deleting:true' property and add 'deleteError:[error]' property to user 
      return {
        ...state,
        items: state.items.map(user => {
          if (user.id === action.id) {
            // make copy of user without 'deleting:true' property
            const { deleting, ...userCopy } = user;
            // return copy of user with 'deleteError:[error]' property
            return { ...userCopy, deleteError: action.error };
          }

          return user;
        })
      };
    default:
      return state
  }
}

user_actions:

export const userService = 
{
    delete: _delete,
};


function _delete(id) {

    const requestOptions = {
        method: 'DELETE',
        // headers: authHeader(),            
    };

    return fetch(`/removeadmin/${id}` , requestOptions).then(handleResponse);
}

AdminListPage组件:

delete(){

this.props.dispatch(userActions.delete(this.state.delete_user._id));

}

此外,在服务器端,我收到成功删除状态

ServerSide Console(200)

Server_Side路由器:

 app.delete('/removeadmin/:id', function(req, res)

{

        var sent_url = url.parse(req.url, true),
            qdata = sent_url.query, 
            sent_id = qdata.id;
        console.log('id ' + sent_id);

        admin.removeadmin(sent_id, function(err, user) {
            if (err)
                throw err;

        });

Server_Side删除功能:

 module.exports.removeadmin = function(id, callback){
     var query = { _id: id };
     Admin.remove(query, callback);
    };

我已经在没有redux的组件中通过简单的fetch命令删除了一个用户,并且我已经在删除请求的主体中发送了id并且它正在工作但是使用redux只是成功的消息。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我发现我在网址上犯了一个错误。所以在URL中的服务器上我无法访问我的id,它显示我未定义。

我刚改变了这些方面: user_action:

return fetch(`/removeadmin?id=${id}` , requestOptions).then(handleResponse);

和server_side路由器:

app.delete('/removeadmin?:id', function(req, res){

答案 1 :(得分:0)

您的帖子中的代码应该有效,但提取请求的网址应该与后端网址一起添加,因此,如果后端网址为localhost:3000,则您的提取应为:

return fetch(`http://localhost:3000/removeadmin/${id}`, 
requestOptions).then(handleResponse);

在您的服务器端路由器中,您可以像这样访问您的id参数:

app.delete('/removeadmin/:id', function(req, res)
{
   var send_id = req.params.id;
    admin.removeadmin(sent_id, function(err, user) {
       if (err)
          throw err;
});