Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't access this.app from within a Hook. #357

Open
SudoPlz opened this issue Jul 3, 2015 · 3 comments
Open

I can't access this.app from within a Hook. #357

SudoPlz opened this issue Jul 3, 2015 · 3 comments

Comments

@SudoPlz
Copy link

SudoPlz commented Jul 3, 2015

This is my code

import Marty from 'marty';
import LoginStore from '../stores/loginStore';
import config from '../config.json';

function createAuthHeader(token) {
    let scheme = config.API.AUTH_SCHEME;
    return `${scheme} token=${token}`;
};

Marty.HttpStateSource.addHook({

    id: 'addToken',


    before(req) {

        let token = this.app.loginStore.getToken(); //error here
        //Uncaught TypeError: Cannot read property 'loginStore' of undefined 


        if (token) {
            req.headers['Authorization'] = createAuthHeader(token);
        }
    }
});

How can I use this.app from within a hook?
Thank you.

@taion
Copy link
Member

taion commented Jul 6, 2015

That's really weird. Are you sure you're properly passing up options to the super constructor of HttpStateSource?

@SudoPlz
Copy link
Author

SudoPlz commented Jul 6, 2015

I have no constructor on HttpStateSource thats the code:

import Marty from 'marty';
import {format} from 'util';
import config from '../config.json';

var base = config.API.ROOT;
var endpoint = base + 'users';

function handleRes(res) {
  let json = res.json();
  if (res.ok) return json;
  throw new Error("Error in response", json, res);
}

class UserHttpAPI extends Marty.HttpStateSource {
  login(email, password) {
    var url = format(endpoint + '/login');
    return this.post({
      url: url,
      body: {
        email: email,
        password: password
      }
    }).then(handleRes);
  }

  getSelf() {
    return this.get(endpoint + '/self').then(handleRes)
  }
}

export default UserHttpAPI;

@taion
Copy link
Member

taion commented Jul 10, 2015

That's really weird. Can you log what this resolves as in your hook?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants