如何从此firebase代码获取返回的结果

时间:2018-02-21 10:57:49

标签: javascript firebase firebase-realtime-database promise

下面的代码完全可以作为firebase函数部署(在return语句中稍作调整),但在完成时无法返回JSON字符串。它会在调用时立即返回一个promise对象,而我无法从中获取任何结果。

function pCreateBuilding(bn, bl, cFloors, cFlats, wmName, wmMobile, ow)
 {

    var funcSuccess = "";
    var funcResult = "";
    var funcError = "";
    var funcWarning = "";
    var funcFailReason = "";

    var buildingsDb = db.collection('BuildingsDB').doc('BldgProfile').collection('Buildings');

    var newbuilding = {
        Name: bn,
        Location: bl,
        Floors: cFloors,
        Flats: cFlats,
        WMName: wmName,
        WMMobile: wmMobile
    };

    // check if a document exists for the mentioned client name...
    var getBuildingDoc = buildingsDb.doc(bn).get()
        .then((snapshot) => {
            if (snapshot.exists) {

                // The client document exists...
                if (ow.toUpperCase() == 'Y') {
                    // Yes, overwrite existsing client document...
                    return buildingsDb.doc(bn).set(newbuilding);
                }
                else {
                    // Do not overwrite... Return the results...
                    funcSuccess = 'No';
                    funcResult = '{}';
                    funcFailReason = 'Building already exists. No Overwrite.';

                    var funcReturn = {
                        Success: funcSuccess,
                        Result: funcResult,
                        FailReason: funcFailReason,
                        Error: funcError,
                        Warning: funcWarning
                    };
                    console.log(JSON.stringify(funcReturn));
                    return JSON.stringify(funcReturn);
                }
            }
            else {
                // No document exists for this client... Create a new document...
                return buildingsDb.doc(bn).set(newbuilding).then((result) => {
                    // Update Buildings Profile and increment the number of clients by 1...
                    var bldgProfileDb = db.collection('BuildingsDB').doc('bldgProfile').get().then((snapshot) => {
                        var bCount = 0;
                        if (snapshot.exists) {
                            // There are fields in the Assets Profile... Get the ClientsCount value;
                            bCount = snapshot.data().BldgCount;

                        }
                        ++bCount;
                        return db.collection('BuildingsDB').doc('BldgProfile').update({ BldgCount: bCount });
                    });
                });
            }
        })
        .then((result) => {
            // On successful writing of client document to database...
            funcSuccess = 'Ok';
            funcResult = '{}';

            var funcReturn = {
                Success: funcSuccess,
                Result: funcResult,
                FailReason: funcFailReason,
                Error: funcError,
                Warning: funcWarning
            };

            console.log(JSON.stringify(funcReturn));
            return JSON.stringify(funcReturn);

        })
        .catch((err) => {
            funcSuccess = 'No';
            funcResult = '{}';
            funcFailReason = 'Error';
            funcError = err.message;

            var funcReturn = {
                Success: funcSuccess,
                Result: funcResult,
                FailReason: funcFailReason,
                Error: funcError,
                Warning: funcWarning
            };

            console.log(JSON.stringify(funcReturn));
            return JSON.stringify(funcReturn);
        });
}

它用于向firestore数据库添加记录,它根据需要执行,但我也想返回流程结果JSON字符串。我在哪里做错了?

请帮忙。 谢谢,

1 个答案:

答案 0 :(得分:0)

我使用async / await和上面的函数如下:

async function pCreateBuilding(bn, bl, cFloors, cFlats, wmName, wmMobile, ow) {
            var funcSuccess = "";
            var funcResult = "";
            var funcError = "";
            var funcWarning = "";
            var funcFailReason = "";
            var res = "";
            var buildingsDb = db.collection('BuildingsDB').doc('BldgProfile').collection('Buildings');

            var newbuilding = {
                Name: bn,
                Location: bl,
                Floors: cFloors,
                Flats: cFlats,
                WMName: wmName,
                WMMobile: wmMobile
            };

            // check if a document exists for the mentioned client name...
            let getBuildingDoc = await buildingsDb.doc(bn).get()
                .then((snapshot) => {
                    if (snapshot.exists) {

                        // The client document exists...
                        if (ow.toUpperCase() == 'Y') {
                            // Yes, overwrite existsing client document...
                            return buildingsDb.doc(bn).set(newbuilding);
                        }
                        else {
                            // Do not overwrite... Return the results...
                            funcSuccess = 'No';
                            funcResult = '{}';
                            funcFailReason = 'Building already exists. No Overwrite.';

                            var funcReturn = {
                                Success: funcSuccess,
                                Result: funcResult,
                                FailReason: funcFailReason,
                                Error: funcError,
                                Warning: funcWarning
                            };
                            // console.log(JSON.stringify(funcReturn));
                            res = JSON.stringify(funcReturn);
                        }
                    }
                    else {
                        // No document exists for this client... Create a new document...
                        return buildingsDb.doc(bn).set(newbuilding).then((result) => {
                            // Update Buildings Profile and increment the number of clients by 1...
                            var bldgProfileDb = db.collection('BuildingsDB').doc('bldgProfile').get().then((snapshot) => {
                                var bCount = 0;
                                if (snapshot.exists) {
                                    // There are fields in the Assets Profile... Get the ClientsCount value;
                                    bCount = snapshot.data().BldgCount;

                                }
                                ++bCount;
                                return db.collection('BuildingsDB').doc('BldgProfile').update({ BldgCount: bCount });
                            });
                        });
                    }
                })
                .then((result) => {
                    // On successful writing of client document to database...
                    funcSuccess = 'Ok';
                    funcResult = '{}';

                    var funcReturn = {
                        Success: funcSuccess,
                        Result: funcResult,
                        FailReason: funcFailReason,
                        Error: funcError,
                        Warning: funcWarning
                    };

                    // console.log(JSON.stringify(funcReturn));
                    res = JSON.stringify(funcReturn);

                })
                .catch((err) => {
                    funcSuccess = 'No';
                    funcResult = '{}';
                    funcFailReason = 'Error';
                    funcError = err.message;

                    var funcReturn = {
                        Success: funcSuccess,
                        Result: funcResult,
                        FailReason: funcFailReason,
                        Error: funcError,
                        Warning: funcWarning
                    };

                    // console.log(JSON.stringify(funcReturn));
                    res = JSON.stringify(funcReturn);
                });
            // console.log(res);
            return res;
        }

然后,我将其称为如下:

pCreateBuilding("DB1", "Al Khuwair", 5, 20, "Annand", 99123456, "Y").then(alert); 

这为我提供了async函数中传递的结果,我现在可以根据需要使用...:)

谢谢大家,我希望这对每个人都有用..