4

I'm learning redux-saga and I'm trying to integrate it into a project that uses an API that is generated with openapi-generator which produces output like the following:

async loginUser(body: Login): Promise<LoginResponse> {
    debugger;
    const response = await this.loginUserRaw({ body: body });
    return await response.value();
}

And loginUserRaw is a function that performs the actual login. Then, I have the following saga:

function* login(action:Login) {
    try{
        const response = yield call(User.loginUser, action);
        yield result(LOGIN_SUCCESS, response)
    }catch(e){
        yield result(LOGIN_FAILURE, e)
    }
}

When this runs, I get an error in my API method's await this.loginUserRaw({ body: body }); line:

TypeError: Cannot read property 'loginUserRaw' of null

I debug it and see that this is null. When I bind the function explicitly in the saga:

const response = yield call(User.loginUser.bind(User), action);

It works, but I don't wanna bind it every time I call a function. How can I get saga to work without explicitly binding the function? (I can't change the generated code and remove this either)

4
  • 1
    Can you post the part of the class that includes loginUserRaw? Oct 15, 2019 at 15:18
  • @JayKariesch if you mean the call to the function, I've already posted it. if you mean the function definition itself, it's just a regular async function that returns a promise. I don't think this is any way relevant though, the problem has nothing to do with the loginUserRaw function itself, it's the parent object reference being null anyway. Oct 15, 2019 at 15:31
  • imo. I'd not make User a class, but rather a module. On the other hand, I have no clue what else User is supposed to handle, and wether that warrants a class. Anyways, try to get rid of the this part in this.loginUserRaw. These are static functions, not methods of some object instance.
    – Thomas
    Oct 15, 2019 at 16:41
  • @Thomas as I've already said, that is autogenerated code, I can't change it. Oct 16, 2019 at 7:03

1 Answer 1

8

Context in Javascript is dynamic based on the way you've written your code

const loginUser = User.loginUser
loginUser() // context lost, because there is no longer `User.` in front of it

Same applies when you pass a function as an parameter. This means that you have to provide the call effect context in some way. The bind method is one option, however the effect itself supports multiple ways of providing context. You can find them in the official docs https://redux-saga.js.org/docs/api/#callcontext-fn-args, here is the short version of it:

Possible ways to pass context:

call([context, fn], ...args)
call([context, fnName], ...args)
call({context, fn}, ...args)

So for example you can do the following:

const response = yield call([User, User.loginUser], action);
5
  • Well, it's still not exactly what I want, but I think there isn't any other option. Oct 16, 2019 at 7:04
  • @CanPoyrazoğlu Yah, I am not huge fan of this either. Another option is just not to use the call effect, but instead call the function directly. That makes testing bit more annoying though + you need to make sure that the called function won't trigger a synchronous store.dispatch. Oct 16, 2019 at 15:23
  • unfortunately the call is async and saga apparently doesn't support async functions Oct 16, 2019 at 17:25
  • Sagas don't use async/await for its API, but it is still normal Javascript so you can use whatever syntax you want for your own stuff. When you call an async function it returns a Promise and saga can handle promises. Just do const response = yield User.loginUser(action); and it will work. Oct 16, 2019 at 18:02
  • What is the downside of writing const response = yield User.loginUser(action); instead of const response = yield call([User, User.loginUser], action);?
    – Ini
    Jul 28, 2022 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.