小编Mat*_*rds的帖子

Angular.js $ http.post TypeError:无法读取未定义的属性"data"

Angular.js v1.0.6

制作$ http.post并收到非200 resposne(在这种情况下为401)

$http.post('http://localhost:3030/auth/login', {
  username: 'username',
  password: 'password'
})
.success(function(data) {
  // Gets called on a 200 response, but not on a 401
  console.log('success');
})
.error(function(err) {
  // Never gets called & dies with error described below.
  console.log('error');
});
Run Code Online (Sandbox Code Playgroud)

Angular会抛出以下错误:

TypeError: Cannot read property 'data' of undefined
    at http://localhost:9000/components/angular/angular.js:8891:22
    at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59)
    at http://localhost:9000/components/angular/angular.js:6834:26
    at Object.Scope.$eval (http://localhost:9000/components/angular/angular.js:8011:28)
    at Object.Scope.$digest (http://localhost:9000/components/angular/angular.js:7876:25)
    at Object.Scope.$apply (http://localhost:9000/components/angular/angular.js:8097:24)
    at done (http://localhost:9000/components/angular/angular.js:9111:20)
    at completeRequest (http://localhost:9000/components/angular/angular.js:9274:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:9000/components/angular/angular.js:9244:11) 
Run Code Online (Sandbox Code Playgroud)

并且从不调用.success()回调或错误.error()回复使得无法处理响应. …

javascript authentication http http-status-code-401 angularjs

28
推荐指数
1
解决办法
3万
查看次数

终极版; 通过combineReducers使用组合减速器时访问该州的其他部分

更新

感谢@Dominic Tobias和@gabdallah发现我令人尴尬的错误.

当然是正确的答案;

所以试试看action.payload.

关于switch语句和action对象的其他注释我们指的是我在我的例子中做出的错误,我已经纠正过了.


想象一下,我结合了以下两个减速器;

import { combineReducers } from 'redux'
import { routerStateReducer } from 'redux-router'
import entries from './entries'

export default combineReducers({
  router: routerStateReducer,
  entries
})
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我想基于全局状态的另一部分来改变条目状态; redux-router提供的路由器状态,以便例如实现分页.

我怎么能这样做?

// entries.js
import { ROUTER_DID_CHANGE } from 'redux-router/lib/constants'
const initialState = {}

function entries (state = initialState, action) {
  switch (action.type) {
    case ROUTER_DID_CHANGE:
      // How can I access `state.router` here in order to do something like this;
      if (routerState.location.pathname === '/entries') {
        return { …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router redux

15
推荐指数
1
解决办法
4383
查看次数

sequelize.js自定义验证器,检查唯一的用户名/密码

想象一下,我已经定义了以下自定义验证器功能:

isUnique: function () { // This works as expected
  throw new Error({error:[{message:'Email address already in use!'}]});
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试查询数据库时遇到问题:

isUnique: function (email) { // This doesn't work
  var User = seqeulize.import('/path/to/user/model');

  User.find({where:{email: email}})
    .success(function () { // This gets called
      throw new Error({error:[{message:'Email address already in use!'}]});  // But this isn't triggering a validation error.
    });
}
Run Code Online (Sandbox Code Playgroud)

如何在自定义验证器中查询ORM并根据ORM的响应触发验证错误?

javascript orm node.js sequelize.js

4
推荐指数
3
解决办法
2万
查看次数