承诺的正确解决方案

时间:2017-10-24 10:07:20

标签: javascript node.js es6-promise google-cloud-firestore

我在node.js上有app连接到firebase。我需要正确更新数据。

如何在promise中调用函数getOrSetUserTrackDay(day)以获得一个好的值,但不是未定义的?

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

1 个答案:

答案 0 :(得分:1)

getOrSetUserTrackDay没有return语句,因此它返回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 {
        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);
});

注意:将getOrSetUserTrackDay从函数声明更改为函数表达式(在这种情况下,箭头函数没有特殊原因) - 因为 Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.