我仍然是Firebase世界的初学者,我一直试图弄清楚下面的代码是什么问题,但我在所有方面都失败了。
代码应该从数据库中的用户配置文件中检索uid
,然后使用它来更新身份验证配置文件,如果身份验证配置文件更新成功,则再次更新数据库配置文件。
在index.js
中,我已经定义了一个导出函数来处理来自HTML表单的POSTed参数。下面的代码定义了另一个模块文件中的处理函数:
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
,更新auth配置文件,然后更新数据库配置文件,但它会一直等待函数返回。
感谢您的宝贵帮助。感谢。
我已更新了以下代码并执行了这些工作但返回了一个空白页面,因为HTTPS在Promise完成之前退出并触发"错误:写完后#34; 错误。
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();
答案 0 :(得分:5)
我在Cloud Functions for Firebase HTTP timeout的上一个答案可能对此有所帮助:
HTTP请求触发的云功能需要终止 以
send()
,redirect()
或end()
结束,否则为他们 将继续运行并达到超时。
从您的代码示例中,您的then(){}
承诺返回看起来以res.end()
结尾,但整个函数从Promise
返回:
return db.collection('Users').where('email', '==', email).get()
当你想要它时,可以阻止它结束。使用HTTPS触发器,您无需返回Promise
以保持函数运行,只需返回结果。
尝试从此行中删除return语句:
db.collection('Users').where('email', '==', email).get()
然后您只需确保所有退出路线(或终止点)以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();
});