Firebase函数返回,并且Promise不会退出该函数

Bad*_*kat 5 javascript node.js firebase firebase-realtime-database google-cloud-functions

我仍然是Firebase领域的初学者,我一直在尝试找出以下代码的问题所在,但是我以所有可能的方式失败了。

该代码应该uid从数据库中的用户概要文件中检索,然后使用它来更新身份验证概要文件,如果身份验证概要文件更新成功,则再次使用它来更新数据库概要文件。

在其中,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();

  });
}
Run Code Online (Sandbox Code Playgroud)

在中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);
Run Code Online (Sandbox Code Playgroud)

我不得不说我可以正常工作来获取uid,更新auth配置文件,然后更新数据库配置文件,但是它一直在等待函数返回。

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


我已经更新了下面的代码,它完成了工作,但是在HTTPS在promise完成之前退出时返回了空白页,从而引发“错误:结束后写入”错误。

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();
Run Code Online (Sandbox Code Playgroud)

Gri*_*orr 7

我以前关于Cloud Functions for Firebase HTTP超时的答案可能对这里有帮助:

通过HTTP请求需要触发云功能,通过与他们结束而终止send()redirect()或者end(),否则他们将继续运行,并达到超时。

从您的代码示例中,您的then(){}诺言返回看起来以结尾res.end(),但是整个函数都Promise从返回:

return db.collection('Users').where('email', '==', email).get()
Run Code Online (Sandbox Code Playgroud)

可能会阻止它在需要时结束。使用HTTPS触发器,您无需返回a Promise即可保持函数运行,而只需返回结果即可。

尝试从此行删除return语句:

db.collection('Users').where('email', '==', email).get()
Run Code Online (Sandbox Code Playgroud)

然后,您只需要确保所有出口路线(或终点)都以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();
  });
Run Code Online (Sandbox Code Playgroud)