简体   繁体   English

在ember的torii-adapter中未定义“ this”

[英]“this” is undefined in torii-adapter in ember

I am trying to store some data post login inside store. 我正在尝试在商店内部存储一些数据登录后登录。 Below is code of my torii-adapter 以下是我的torii-adapter的代码

import Ember from 'ember';
import {createToken} from 'myapp/utils/app-utils';

export default Ember.Object.extend({
  store: Ember.inject.service(),

  open: function(authentication){
    let authorizationCode = authentication.authorizationCode;
    let token = createToken();
    return new Ember.RSVP.Promise(function(resolve, reject){
      console.log(authentication);
      Ember.$.ajax({
        url: 'http://localhost/getUserInfoWuthAuthCode.php',
        data: { 'code': authorizationCode,token:token},
        success: Ember.run.bind(null, resolve),
        error: Ember.run.bind(null, reject)
      });
    }).then(function(data){
      let user = data.user[0];
      this.set('storage.token',token); //this is undefined
      return {
        user: user
      };
    });
  }

});

Error i am getting is "TypeError: Cannot read property 'set' of undefined". 我得到的错误是“ TypeError:无法读取未定义的属性'set'”。 I am injecting the store service as well. 我也在注入商店服务。 Could you please tell me what exactly is going wrong here? 您能告诉我这里到底出了什么问题吗?

this is not defined in your current context. this不是在您当前的上下文中定义的。 Assign it to another varible. 将其分配给另一个变量。

  open: function(authentication){
    let authorizationCode = authentication.authorizationCode;
    let token = createToken();
    let that = this;
    return new Ember.RSVP.Promise(function(resolve, reject){
      console.log(authentication);
      Ember.$.ajax({
        url: 'http://localhost/getUserInfoWuthAuthCode.php',
        data: { 'code': authorizationCode,token:token},
        success: Ember.run.bind(null, resolve),
        error: Ember.run.bind(null, reject)
      });
    }).then(function(data){
      let user = data.user[0];
      that.set('storage.token',token); //this is undefined
      return {
        user: user
      };
    });
  }

To get further understanding and to see different options (such as fat arrow or bind method), have a look at this question . 为了进一步理解并查看其他选项(例如粗箭头bind方法),请查看此问题

Another approach is to use ES6 arrow function, so that you do not need to assign this other another variable. 另一种方法是使用ES6箭头功能,让您无需指定this之外的另一个变量。

 open: function(authentication){
    let authorizationCode = authentication.authorizationCode;
    let token = createToken();
    let that = this;
    return new Ember.RSVP.Promise((resolve, reject) => {
      console.log(authentication);
      Ember.$.ajax({
        url: 'http://localhost/getUserInfoWuthAuthCode.php',
        data: { 'code': authorizationCode,token:token},
        success: Ember.run.bind(null, resolve),
        error: Ember.run.bind(null, reject)
      });
    }).then((data) => {
      let user = data.user[0];
      that.set('storage.token',token);
      return {
        user: user
      };
    });
  }

Ember-cli will help you to convert ES6 to ES5 to make it compatible for most browsers. Ember-cli将帮助您将ES6转换为ES5,以使其与大多数浏览器兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM