[SOLVED] Impossible to make promises on the react side with meteor methods

Hello, I use React and Redux with meteor, I have a problem I cannot do then, react side

for example for the register page I do this:, “the registration is going well”

// page register
const registerUser = (values) => {
    return register(values)
      .then((res) => console.log('res', res))
      .catch((err) => {
        setError(err);
      });
  };

redux part

export const register = (data) => (dispatch) => {
  return Accounts.createUser(data, function (error, result) {
    if (error) {
      dispatch({
        type: REGISTER_FAIL,
        payload: error.reason,
      });
      return error;
    } else {
      dispatch({
        type: REGISTER_SUCCESS,
      });
      return result;
    }
  });
};

I come across this error:

RegisterPage.jsx:22 Uncaught TypeError: Cannot read property 'then' of undefined

You are passing a callback into Accounts.createUser. I don’t think that will return a promise. If that is what you want to do you need to wrap the body of register in a Promise and return resolve(result) in the body. Return the new Promise from register.

2 Likes

did you have an example please, sorry with the translation i don’t understand everything

I succeeded by doing this, thanks for your help

export const register = (data) => (dispatch) => {
  return new Promise((resolve, reject) => {
    Accounts.createUser(data, (error, result) => {
      if (error) {
        reject(error.reason);
        dispatch({
          type: REGISTER_FAIL,
          payload: error.reason,
        });
      } else {
        resolve(result);
        dispatch({
          type: REGISTER_SUCCESS,
        });
      }
    });
  });
};
3 Likes

Yep you got it! Nice work.