【发布时间】:2019-07-29 18:57:25
【问题描述】:
编辑**
好的,由于提供的第一个答案,我能够使参数正常工作,但现在我遇到了一个问题,即我的函数完全在 Firebase 中创建一个新用户,而不是更新现有用户,即我传入的 uid auth.admin.updateUser 是我要更新的电子邮件的现有用户的 uid。这是更新的云功能,它是添加新用户而不是更新现有用户:
exports.updateEmail = functions.https.onCall((data, context) => {
const email = data.email;
const uid = data.uid;
admin.auth().updateUser(uid, {
email: email
})
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log("Successfully updated user", userRecord.toJSON());
return response.status(200).json(userRecord.toJSON());
})
.catch(function(error) {
console.log("Error updating user:", error);
return response.status(404).json({
error: 'Something went wrong.'
});
});
});
我从 firebase 文档中获得了这个功能,但它并没有按照我的预期去做。
原帖**
从我的颤振代码中调用云函数时,我在让云函数工作时遇到了一些困难。我遇到的问题是 uid 和 email 字段未定义,即使我使用 busboy 字段将它们传递给云函数。
我正在尝试将电子邮件和 uid 字段传递给函数,如下所示:
final request = http.MultipartRequest('POST', Uri.parse('****************my function url************'));
request.fields['email'] = Uri.encodeComponent(newEmail);
request.fields['uid'] = Uri.encodeComponent(selectedUser.uid);
request.headers['Authorization'] = 'Bearer ${_authenticatedUser.token}';
final http.StreamedResponse streamedResponse = await request.send();
在 Node.js 方面,我正在尝试使用 busboy 来使用这些字段,这是我在 Node.js 中的云功能:
exports.changeEmail = functions.https.onRequest((request, response) => {
if (!request.headers.authorization ||
!request.headers.authorization.startsWith('Bearer ')
) {
return response.status(401).json({
error: 'Unauthorized.'
});
}
let idToken;
idToken = request.headers.authorization.split('Bearer ')[1];
let email;
let uid;
const busboy = new Busboy({
headers: request.headers
});
busboy.on('field', (fieldname, value) => {
if (fieldname == 'email') {
email = decodeURIComponent(value);
}
if (fieldname == 'uid') {
uid = decodeURIComponent(value);
}
});
admin.auth().updateUser(uid, {
email: email
})
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log("Successfully updated user", userRecord.toJSON());
return response.status(200).json(userRecord.toJSON());
})
.catch(function(error) {
console.log("Error updating user:", error);
return response.status(404).json({
error: 'Something went wrong.'
});
});
});
即使我将字段与 busboy 字段一起传递,但它们没有在函数中设置,我在这里做错了吗?
【问题讨论】:
标签: firebase flutter google-cloud-firestore google-cloud-functions busboy