0

I am using NodeJs and try to collect data from cloud Firestore. I want to compare the returned value from GetDataPassword function with a string, but there is a problem. My function working properly, but when i did compare it to string, it always not match (the returned value (xxxxx) and the string "xxxx"), and always go to "else". And i figure the type of returned value, it said object. but sometimes it returned {}. Please help me.

const loginUser = (request, h) => {
  const { username, password } = request.payload;

  async function GetDataUsername(db) {
    const dataUsername = db.collection('users').doc('' + username);
    const uname = await dataUsername.get().then(function (doc) {
      return doc.data().username;
    });
    return uname;
  }

  async function GetDataPassword(db) {
    const dataUsername = db.collection('users').doc('' + username);
    const pass = await dataUsername.get().then(function (doc) {
      return doc.data().password;
    });
    return pass;
  }
  const dataUsername = GetDataUsername(db);
  const dataPassword = GetDataPassword(db);

  if (username == dataUsername && password == dataPassword) {
    return h
      .response({
        status: 'success',
        message: 'Login berhasil',
        data: {
          username,
        },
      })
      .code(200);
  }
  const response = h
    .response({
      status: 'fail',
      message: 'Login gagal',
    })
    .code(400);
  return response;
};
  • 1
    `GetDataUsername` and `GetDataPassword` are `async`, but you are not `await` ing them – derpirscher Jun 06 '21 at 16:21
  • 1
    Does this answer your question? [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – derpirscher Jun 06 '21 at 16:21
  • ehmm, i wrote await in const uname and const pass, is it not enough for fixin' the async func? – Ilyas Adiyasa Jun 06 '21 at 16:36
  • I've tried using Pomise.all(..) but it still didn't work. – Ilyas Adiyasa Jun 06 '21 at 16:36
  • The code you show above, doesn't have any `await`. Furthermore, the function scope where you are calling `GetDataUsername` and `GetDataPassword` is not async, thus, you can't use `await` there. `Promise.all()` won't help either, if you are not awaiting it. – derpirscher Jun 06 '21 at 16:39

1 Answers1

0

You don't need to use .then() if you are using await. Also both the function GetDataUsername and GetDataPassword return promises hence you should use await.

const loginUser = async (request, h) => {
    //Do note the ^^^^^ async 
    const { username, password } = request.payload;
  
    async function GetDataUsername(db) {
      const dataUsername = db.collection('users').doc('' + username);
      const uname = (await dataUsername.get()).data().username
      return uname;
    }
  
    async function GetDataPassword(db) {
      const dataUsername = db.collection('users').doc('' + username);
      const pass = (await dataUsername.get()).data().password
      return pass;
    }
    const dataUsername = await GetDataUsername(db);
    const dataPassword = await GetDataPassword(db);
  
    if (username == dataUsername && password == dataPassword) {
      return h
        .response({
          status: 'success',
          message: 'Login berhasil',
          data: {
            username,
          },
        })
        .code(200);
    }
    const response = h
      .response({
        status: 'fail',
        message: 'Login gagal',
      })
      .code(400);
    return response;
  };

That should do. Let me know if that doesn't work and we'll debug :)

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • that's work, but when i tried to try another username that not registered, it said error that username is undefined. – Ilyas Adiyasa Jun 07 '21 at 04:17
  • @IlyasAdiyasa check the document you are fetching if that has username is defined. It is case sensitive. Maybe update your question with updated code and error. – Dharmaraj Jun 07 '21 at 04:19