Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ember:TypeError:无法读取属性';设置';未定义的_Javascript_Ember.js - Fatal编程技术网

Javascript ember:TypeError:无法读取属性';设置';未定义的

Javascript ember:TypeError:无法读取属性';设置';未定义的,javascript,ember.js,Javascript,Ember.js,我正在尝试将一个值设置到userSessionStorage,当我从authenticate()函数访问它时,它似乎工作正常 但是,它在.then()承诺中不起作用 app/controllers/show.js import Ember from 'ember'; import { storageFor } from 'ember-local-storage'; export default Ember.Controller.extend({ session: Ember.inject.

我正在尝试将一个值设置到
userSessionStorage
,当我从authenticate()函数访问它时,它似乎工作正常

但是,它在.then()承诺中不起作用

app/controllers/show.js

import Ember from 'ember';
import { storageFor } from 'ember-local-storage';

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),

  userSessionStorage: storageFor('user'),

  authenticator: 'authenticator:custom',

  actions: {
    authenticate: function() {
      var credentials = this.getProperties('identification', 'password');
      // ok
      this.set('userSessionStorage.username', credentials.identification);

      this.get('session').authenticate('authenticator:custom', credentials)
        .then(function(){

          // error: TypeError: Cannot read property 'set' of undefined
          this.set('userSessionStorage.username', credentials.identification);

        })
        .catch((message) => {
          console.log("message: " + message);

          this.controller.set('loginFailed', true);
        });
    }
  }
});

它基于我自己的代码,所以您可能需要修改它。但是在验证器中,
authenticate
函数可能如下所示:

# authenticator

authenticate(credentials) {
  const { identification, password } = credentials;
  const data = JSON.stringify({
    auth: {
      email: identification,
      password
    }
  });

  const requestOptions = {
    url: this.tokenEndpoint,
    type: 'POST',
    data,
    contentType: 'application/json',
    dataType: 'json'
  };

  return new Promise((resolve, reject) => {
    ajax(requestOptions).then((response) => {
      // Set your session here
    }, (error) => {
      run(() => {
        reject(error);
      });
    });
  });
},

它基于我自己的代码,所以您可能需要修改它。但是在验证器中,
authenticate
函数可能如下所示:

# authenticator

authenticate(credentials) {
  const { identification, password } = credentials;
  const data = JSON.stringify({
    auth: {
      email: identification,
      password
    }
  });

  const requestOptions = {
    url: this.tokenEndpoint,
    type: 'POST',
    data,
    contentType: 'application/json',
    dataType: 'json'
  };

  return new Promise((resolve, reject) => {
    ajax(requestOptions).then((response) => {
      // Set your session here
    }, (error) => {
      run(() => {
        reject(error);
      });
    });
  });
},

您只需更改以下行:

 this.get('session').authenticate('authenticator:custom', credentials)
        .then(function(){....}
要使用胖箭头符号,请执行以下操作:

 this.get('session').authenticate('authenticator:custom', credentials)
        .then(()=>{....}

因此,promise上下文中的
上下文将成为您的控制器。有关ES6箭头功能的更多信息,请参阅。

您只需更改以下行:

 this.get('session').authenticate('authenticator:custom', credentials)
        .then(function(){....}
要使用胖箭头符号,请执行以下操作:

 this.get('session').authenticate('authenticator:custom', credentials)
        .then(()=>{....}

因此,promise上下文中的
上下文将成为您的控制器。有关ES6 arrow函数的更多信息,请参阅。

将要传递给
的函数重写为一个arrow函数,该函数将保持
的周围值。或者使用
const self=this在
authenticate
方法顶部捕获
this
的该值然后使用
self.set
@torazaburo你很好:)你能把这个作为答案,这样我就可以把它作为正确的答案吗?为什么不在你的验证器中设置
userSessionStorage.username
,也许你能把它作为一个答案吗?把你传递给
的函数重写成一个箭头函数,它保持
这个
的周围值。或者使用
const self=this在
authenticate
方法顶部捕获
this
的该值然后使用
self.set
@torazaburo你很好:)你能把这个作为答案,这样我就可以把它作为正确的答案吗?为什么不在你的验证器中设置
userSessionStorage.username
?@j-samah你能详细说明一下吗,也许你能把它作为答案吗?