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;
};