0

I'm trying to create this promise:

const getTocStatus = new Promise((resolve, reject) => {
  const userInfo = Auth.currentUserInfo();
  resolve(userInfo.attributes['custom:tocStatus']);
  reject(new Error('Couldn\'t connect to Cognito'));
});

Then use it like this:

getTocStatus.then((response) => {
  if (response === 'pending) { //do sth }
}, error => console.log('Error:', error)

But I'm getting the Error:

[TypeError: undefined is not an object (evaluating 'userInfo.attributes['custom:tocStatus']')]

What is badly coded on the promise and it call?

pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • The error doesn't have anything to do with promises, it's saying that `userInfo.attributes` is undefined. Is `userInfo` an object and does it have an `attributes` object on it? – Zac Anger Jan 25 '21 at 02:51
  • Yes, when I use await/async I can see the console.log for that const userInfo is an object with that property – pmiranda Jan 25 '21 at 02:52
  • Ah, see Lionel's answer. I didn't know what Auth.currentUserInfo was, but it looks like it's an Amplify library and it returns a Promise. – Zac Anger Jan 25 '21 at 02:56

4 Answers4

2

The problem is that Auth.currentUserInfo gives you a promise, not a value, so you need to wait for it to complete before you can return its contents. Mario Vernari is also correct in that your error handling has problems too, but that's not why your code is crashing. This should hopefully fix both problems.

const getTocStatus = new Promise(async (resolve, reject) => {
  try {
    const userInfo = await Auth.currentUserInfo();
    resolve(userInfo.attributes['custom:tocStatus']);
  } catch (e) {
    reject(new Error('Couldn\'t connect to Cognito'));
  }
});
Lionel Foxcroft
  • 1,584
  • 3
  • 9
  • 23
2

Lionel's answer is correct (I didn't know what Auth.currentUserInfo was, but there's no need for the Promise constructor since you're already dealing with promises:

const getTocStatus = async () => {
  try {
    const userInfo = await Auth.currentUserInfo()
    return userInfo.attributes['custom:tocStatus']
  } catch (e) {
    new Error("Couldn't connect to Cognito")
  }
}

// or with .then syntax
const getTocStatus = () =>
  Auth.currentUserInfo()
    .then((userInfo) => userInfo.attributes['custom:tocStatus'])
    .catch((e) => { Promise.reject(new Error("Couldn't connect to Cognito")) })
Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • Thanks for the answer and explanation, I will post what I could do before reading this answers, I will fix my code, plus, that then/catch on Auth.currentUserInfo was what I need so I'll pick your answe as the best one – pmiranda Jan 25 '21 at 03:04
1

You must discriminate when there's an error and when it's not:

const getTocStatus = new Promise((resolve, reject) => {
  try {
    const userInfo = Auth.currentUserInfo();
    resolve(userInfo.attributes['custom:tocStatus']);
  }
  catch (err) {
    reject(new Error('Couldn\'t connect to Cognito'));
  }
});

...or something like that.

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
0

Finally I did this, but I will fix my code using this:

const getTocStatus = new Promise((resolve, reject) => {
  try {
    Auth.currentUserInfo()
      .then(response => {
        resolve(response.attributes['custom:tocStatus'] || TocStatus.CONFIRMED);
      })
      .catch(err => console.log(err));
  } catch (err) {
    reject(new Error('Couldn\'t connect to Cognito'));
  }
});

And:

getTocStatus.then((response) => {
  console.log('response dentro del error', response);
  if (response === 'pending') {
    // do sth
  }
}, error => console.log(error)
pmiranda
  • 7,602
  • 14
  • 72
  • 155