下面的代码在firebase函数部署(在返回语句中稍有调整(中完美工作,但完成后无法返回JSON字符串。它立即返回一个呼叫对象,我无法从中获得任何结果。
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字符串。我在哪里做错?
请帮助。谢谢,
我使用异步/等待上述功能如下:
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);
这为我提供了从异步函数进行的结果,我现在可以根据需要使用该功能...:(
谢谢大家,我希望这对每个人都会有用。