0

I have tried other similar questions on StackOverflow but I found no solution to my problem.

Problem is that I am making an API call inside a function, I am also using async/await for this call but the function still returning {"_U": 0, "_V": 0, "_W": null, "_X": null}.

From this and other questions on StackOverflow, I came to know that this problem arises because the function not working properly with API.

import { firebase } from '@react-native-firebase/firestore';

async function userLogin(name, password) {
    await firebase.firestore().collection('users').where("name", "==", name).where("password", "==", password).get()
        .then((response) => {
            console.log('Response:')
            if (response.docs === []) {
                return
            } else {
                return true
            }
        }).catch(err => console.error(err));
}

const initialState = {
    loggedIn: false,
    name: '',
}

const rootReducer = (state = initialState, action) => {
    if (action.type === 'LogIn') {
        const data = userLogin(action.name, action.password)
        if (data) {
        return {
            ...state,
            name:action.name,
            loggedIn:false
            }
        }
        else {
            console.log(4);
            return {
                ...state,
                name: 'aName',
                loggedIn:true
            }
            }
    }
    return state;
}

export default rootReducer;
yogesh kumar
  • 103
  • 10
  • 2
    `userLogin` returns a promise. `data` contains a promise. I think, you can't use asynchronous functions in a reducer. – jabaa Feb 16 '22 at 12:18
  • In addition to @jabaa's comment, try calling userLogin func outside reducer and on the success resposne dispatch the Login action – Thanhal P A Feb 16 '22 at 12:25

1 Answers1

-1

userLogin not return anything in your code. Try:

async function userLogin(name, password) {
  return await firebase.firestore().collection('users').where("name", "==", name).where("password", "==", password).get()
  ...

To be precise, according to jabaa comment, userLogin returns null wrapped in Promise, which looks like

{"_U": 0, "_V": 0, "_W": null, "_X": null}

Try add return and then check data constant. That give you response from firestore method.

karror
  • 21
  • 3
  • _"Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise."_ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function – jabaa Feb 16 '22 at 12:46
  • Yes, thats true, but if you don't set returned value, it will response with null wrapped in promise, like in your case: {"_U": 0, "_V": 0, "_W": null, "_X": null}. Thats why const data = userLogin(action.name, action.password) setting 'data' to nulled promise instead firebase method response. – karror Feb 17 '22 at 13:11
  • But your answer is wrong: _"userLogin not return anything in your code"_. `userLogin` returns a promise. Your answer won't fix the problem. `data` is still a promise.`if (data)` won't work. – jabaa Feb 17 '22 at 15:22