TypeError: Cannot read property 'then' of undefined

There is a Token file with the request

export default async function Token(email, password) {
  if (!res.ok) {
    throw new Error('Could not fetch');
  }
  return fetch(`/api/login?pass=${password}&user=${email}`, {
    method: 'POST',
  })
}

I'm importing the Token function into login.js

import Token from '../requests';
import saveToken from '../token';

export default function login(email, password) {
  Token(`/api/login?pass=${password}&user=${email}`, {
      method: 'POST'
    })
    .then((res) => res.text())
    .then(saveToken)
    .catch(() => {
      alert('failed');
    });
}

After that, I hang this request on the authorization button and this error TypeError: Cannot read property 'then' of undefined arrives at this place.

login(email, password)
      .then(() => {
        history.push('/home');
      });

Any help would be appreciated.

Author: quires, 2020-01-16

1 answers

As written in the comments, the login function returns undefined, which does not have a then method. You must explicitly return Token

import Token from '../requests';
import saveToken from '../token';

export default function login(email, password) {
  return Token(`/api/login?pass=${password}&user=${email}`, {
      method: 'POST'
    })
    .then((res) => res.text())
    .then(saveToken)
    .catch(() => {
      alert('failed');
    });
}

login(...).then(...);
 1
Author: ThisMan, 2020-01-16 12:15:56