Firebase函数返回并承诺不会退出该函数

我仍然是Firebase世界的初学者,我一直在试图弄清楚下面的代码是什么问题,但是我没有任何可能的办法。

代码应该从数据库中的用户configuration文件中检索uid ,然后使用它来更新authenticationconfiguration文件,然后在authenticationconfiguration文件更新成功时再次更新数据库configuration文件。

index.js我定义了一个导出的函数来处理来自HTML表单的POST参数。 下面的代码定义了另一个模块文件中的处理函数:

  exports.auUpdateUserByEmail = (req, res) => { // This handler function will retrieve the POSTed params of user profile // and will attempt to update the existing user authentication as well as // the database profiles. // // This function accepts the following params: // 1. User email // 2. Phone number // 3. password // 4. Display name // 5. Photo url // 6. Disabled Flag // var db = admin.firestore(); var uEmail = req.body.userEmail; var dName = req.body.displayName; var userId = ""; var newuser = { displayName: dName } console.log("Email passed: " + uEmail); // Fetch the user UID by user email... res.write('User UID: ' + userId); console.log('User UID: ' + userId); // attempt to update the user authentication profile... return db.collection('Users').where('email', '==', email).get() .then(snapshot => { snapshot.forEach(doc => { var d = doc.data(); console.log("doc.id: " + doc.id + " - d.email: " + d.email); if(d.email == email) { userId = d.uid; } }); return admin.auth().updateUser(userId, newuser); }).then(function(userRecord) { // The updating was successful... Attempt to update User Details in // database User Profile... console.log("User Updated Successfully. UID: " + userRecord.uid); retUid = userRecord.uid; // Create a reference to the Users Database... var docRef = db.collection('Users'); // Update the user profile document. return docRef.doc(userRecord.uid).update(newuser); }).then(result => { // everything went fine... return User UID as a successful result... res.write(userId); return res.end(); }).catch(function(error) { console.log("doc.update - Error updating user profile in database:", error); return res.end(); }); } 

index.js ,我有以下导出定义:

 var appAuth = express(); //Here we are configuring express to use body-parser as middle-ware. appAuth.use(bodyParser.urlencoded({ extended: false })); appAuth.use(bodyParser.json()); appAuth.post('/updateUserByEmail', authusers.auUpdateUserByEmail); exports.usersAuthFunctions = functions.https.onRequest(appAuth); 

我不得不说,我得到它的工作正常,以获得uid ,更新身份validationconfiguration文件,然后更新数据库configuration文件,但它一直在等待函数返回。

感谢您宝贵的帮助。 谢谢。


我已经更新了下面的代码,它做了这些工作,但是返回一个空白页面,因为在承诺完成之前HTTPS退出,这会触发“错误:写完后”错误。

 var fetch_uid = db.collection('Users').where('email', '==', uEmail).get() .then(snapshot => { // var userId = snapshot.data.uid; snapshot.forEach(doc => { var d = doc.data(); console.log("doc.id: " + doc.id + " - d.email: " + d.email); if(d.email == uEmail) { userId = d.uid; res.write('User UID: ' + userId); console.log('User UID: ' + userId); } }); return admin.auth().updateUser(userId, newuser); }).then(function(userRecord) { // The updating was successful... Attempt to update User Details in // database User Profile... console.log("User Updated Successfully. UID: " + userRecord.uid); retUid = userRecord.uid; // Create a reference to the Users Database... var docRef = db.collection('Users'); // Update the user profile document. return docRef.doc(userRecord.uid).update(newuser); }).then(result => { // everything went fine... return User UID as a successful result... res.write(userId); return; }).catch(function(error) { console.log("doc.update - Error updating user profile in database:", error); return; }); res.end(); 

我之前对Firebase HTTP超时云function的回答可能对此有所帮助:

由HTTP请求触发的云function需要通过send()redirect()end()结束来终止,否则它们将继续运行并达到超时。

从你的代码示例看来,你的then(){} promise返回以res.end()结束,但是整个函数将Promise从:

 return db.collection('Users').where('email', '==', email).get() 

这可能会阻止它结束,当你想要的。 使用HTTPS触发器,您不需要返回一个Promise来保持函数运行,只需要一个结果。

尝试从这一行中删除return语句:

 db.collection('Users').where('email', '==', email).get() 

那么你只需要确保所有的退出path(或终止点)以res.end()或类似的结束,所以目前你有2个终止点:

  }).then(result => { // everything went fine... return User UID as a successful result... res.write(userId); res.status(200).end(); }).catch(function(error) { console.log("doc.update - Error updating user profile in database:", error); res.status(500).end(); });