Roman Melnyk
Roman Melnyk

Reputation: 190

The right solution for promises

I have the app on node.js with connecting to firebase. I need to update the data correctly.

How to call the function getOrSetUserTrackDay(day) in a promise to get a good value, but not undefined?

let userData = [];
let userID = req.params.userID; 
let today = req.params.today;
let yesterday = req.params.yesterday;

db.collection('users').doc(userID).get()
    .then((userDataFromDB) => {
        if (!userDataFromDB.exists) {
            res.status(404).send('User not found');
        }
        else {
            function getOrSetUserTrackDay(day) {

                let userTrackRef = db.collection('history').doc('daily').collection(day).doc(userID);
                userTrackRef.get()
                    .then(userTrackData => {
                        if (userTrackData.exists) {
                            return userTrackData.data(); // good
                        }
                        else {
                            let userNewData = {
                                username: userDataFromDB.data().username,
                                photoUrl: userDataFromDB.data().photoUrl
                            };
                            userTrackRef.update(userNewData);
                            return userNewData; // good
                        }
                    })
            }

            userData = {
                user: userDataFromDB.data(),
                today: getOrSetUserTrackDay(today), // undefined
                yesterday: getOrSetUserTrackDay(yesterday) // undefined
            };
            res.json(userData);
        }
    })
    .catch((err) => {
        console.log(err);
        res.status(404).send(err);
    });

Upvotes: 2

Views: 59

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

well getOrSetUserTrackDay has no return statement, hence it returns undefined - but, since it contains asynchronous code, you'll never be able to use it synchronously

So, you can do the following

let userData = [];
let userID = req.params.userID; 
let today = req.params.today;
let yesterday = req.params.yesterday;
db.collection('users').doc(userID).get()
.then((userDataFromDB) => {
    if (!userDataFromDB.exists) {
        res.status(404).send('User not found');
    }
    else {
        let getOrSetUserTrackDay = day => {
            let userTrackRef = db.collection('history').doc('daily').collection(day).doc(userID);
            return userTrackRef.get()
            .then(userTrackData => {
                if (userTrackData.exists) {
                    return userTrackData.data(); // good
                } else {
                    let userNewData = {
                        username: userDataFromDB.data().username,
                        photoUrl: userDataFromDB.data().photoUrl
                    };
                    userTrackRef.update(userNewData);
                    return userNewData; // good
                }
            });
        };
        Promise.all([getOrSetUserTrackDay(today), getOrSetUserTrackDay(yesterday)])
        .then(([today, yesterday]) => res.json({
            user: userDataFromDB.data(),
            today,
            yesterday
        }));
    }
}).catch((err) => {
    console.log(err);
    res.status(404).send(err);
});

Note: changed getOrSetUserTrackDay from a function declaration to a function expression (in this case, an arrow function for no particular reason) - because Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.

Upvotes: 1

Related Questions