L
L
Lynatik0012020-06-29 23:36:55
JavaScript
Lynatik001, 2020-06-29 23:36:55

Mongoose promises and async - await (and before that it just returned emptiness), but now why does the promise return?

async finduser(id) {
    const us = await UserScheme.findOne({ id });
    console.log('fck'); -- кстати это не отображает в консоли
    return us;
  }


checkAddUser(user) {
    console.log('us .');
    const ff = this.finduser(user.id);

    const res = ff.length ? ff[0].id : undefined;
    console.log('u3s .' + ff);
    if (res === undefined) {
      console.log(`${user.id} add new user`);
      this.adduser(user.id, user.first_name, user.last_name, user.username);
      return false;
    }
    if (ff[0].city != null) {
      return ff;
    }

    console.log(`${user.id} authorized`);
    return ff;
  }


How else to catch all sorts of errors and so on. competently here in the code (I read Google, but it didn’t fit this situation (when the function returned emptiness at all, + some console logs didn’t work after it))

I’ll still use moleculer. Can its means handle errors and other easier?
By the way, what does findOne return if it doesn't find findOne?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-06-29
@Rsa97

an async function returns a Promise, so you don't have a string in ff.
To get a row, you need to use await when calling this.finduser, for which you declare checkAddUser as async.

L
Lynatik001, 2020-06-30
@Lynatik001

if anything, then I solved the problem, my Eslint + prettier ate an unused object during autosave. and this object initialized the connection to the database
True, xs, why the hell * didn’t write a single hint .....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question