在 Firebase 云函数中使用 getUserByEmail
Using getUserByEmail in a Firebase cloud function
在我正在开发的 Firebase 网络应用程序中,我想从邮件地址获取用户 ID。为此,我正在尝试编写一个云函数。但它不起作用,或者我没有正确使用它。这是当前代码(基于我在网上找到的一些示例):
"use strict";
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp(functions.config().firebase);
exports.myFunc = functions.https.onRequest(function(req, resp) {
const from = req.body.sender;
admin.auth().getUserByEmail(from)
.then(function(userRecord) {
console.log("Successfully fetched user data:", userRecord.toJSON());
})
.catch(function(error) {
console.log("Error fetching user data:", error);
});
});
运行“firebase 部署”时我没有发现任何问题。
然后我尝试测试该功能,就像我在 this tutorial.
之后编写的演示应用程序所做的那样
例如(现有和不存在的邮件地址):
https://us-central1-myapp.cloudfunctions.net/myFunc
https://us-central1-myapp.cloudfunctions.net/myFunc?from=abcd@xyz.com
但在任何一种情况下,我在 Web 控制台和浏览器中都什么也得不到:
Error: could not handle the request
欢迎更有经验的 Firebase 云函数用户提出任何建议。
事实上,一些带有示例代码的教程展示了如何在 firebase 云函数中使用 getUserByEmail 将是最好的。
..... 这里有更多信息,经过进一步调查 .....
使用此代码时:
const from = req.body.sender;
admin.auth().getUserByEmail(from)
我在日志中收到此错误:
Error fetching user data: FirebaseAuthError: The email address is improperly formatted.
当使用其中一行代码时:
admin.auth().getUserByEmail("fakemail@example.com")
admin.auth().getUserByEmail("realmail@example.com")
我在日志中得到了预期的结果:
第一行(带有虚假邮件):
Error fetching user data: FirebaseAuthError: There is no user record corresponding to the provided identifier.
第二行(实际存在的邮件):
Successfully fetched user data: {
2022-01-22T07:25:04.946Z ? myFunc: uid: 'yfC123abc....',
2022-01-22T07:25:04.946Z ? myFunc: email: 'realmail@example.com',
.....
}
我需要知道如何正确地让这个函数接受一个参数(称为 from 或其他),并且知道如何使用我原来的网络应用程序中的函数.
...经过更多的尝试和错误后还有更多代码 ...
这是显示我当前问题的新代码块:
const from = req.query.from;
// The 2 following lines produce the expected result.
// That is the mail address passed as a parameter.
console.log("from(CL):", from);
functions.logger.log("from(FL):", from);
admin.auth().getUserByEmail(from)
.then(function(userRecord) {
我收到一条错误消息,当 运行 firebase deploy 与 getUserByEmail 阅读:
src/index.ts:37:33 - error TS2345: Argument of type 'string | string[] | ParsedQs | ParsedQs[] | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
37 admin.auth().getUserByEmail(from)
~~~~
用于处理 getUserByEmail 调用的行的正确语法是什么。
问题好像是req.body
...
同时将输入作为 query-string 传递:?from=abcd@xyz.com
.
我假设 req.query.from
应该携带一个值,而设置。
我不得不使用这个:
const from = String(req.query.from);
而不是:
const from = req.query.from;
在我正在开发的 Firebase 网络应用程序中,我想从邮件地址获取用户 ID。为此,我正在尝试编写一个云函数。但它不起作用,或者我没有正确使用它。这是当前代码(基于我在网上找到的一些示例):
"use strict";
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp(functions.config().firebase);
exports.myFunc = functions.https.onRequest(function(req, resp) {
const from = req.body.sender;
admin.auth().getUserByEmail(from)
.then(function(userRecord) {
console.log("Successfully fetched user data:", userRecord.toJSON());
})
.catch(function(error) {
console.log("Error fetching user data:", error);
});
});
运行“firebase 部署”时我没有发现任何问题。 然后我尝试测试该功能,就像我在 this tutorial.
之后编写的演示应用程序所做的那样例如(现有和不存在的邮件地址):
https://us-central1-myapp.cloudfunctions.net/myFunc
https://us-central1-myapp.cloudfunctions.net/myFunc?from=abcd@xyz.com
但在任何一种情况下,我在 Web 控制台和浏览器中都什么也得不到:
Error: could not handle the request
欢迎更有经验的 Firebase 云函数用户提出任何建议。
事实上,一些带有示例代码的教程展示了如何在 firebase 云函数中使用 getUserByEmail 将是最好的。
..... 这里有更多信息,经过进一步调查 .....
使用此代码时:
const from = req.body.sender;
admin.auth().getUserByEmail(from)
我在日志中收到此错误:
Error fetching user data: FirebaseAuthError: The email address is improperly formatted.
当使用其中一行代码时:
admin.auth().getUserByEmail("fakemail@example.com")
admin.auth().getUserByEmail("realmail@example.com")
我在日志中得到了预期的结果:
第一行(带有虚假邮件):
Error fetching user data: FirebaseAuthError: There is no user record corresponding to the provided identifier.
第二行(实际存在的邮件):
Successfully fetched user data: {
2022-01-22T07:25:04.946Z ? myFunc: uid: 'yfC123abc....',
2022-01-22T07:25:04.946Z ? myFunc: email: 'realmail@example.com',
.....
}
我需要知道如何正确地让这个函数接受一个参数(称为 from 或其他),并且知道如何使用我原来的网络应用程序中的函数.
...经过更多的尝试和错误后还有更多代码 ...
这是显示我当前问题的新代码块:
const from = req.query.from;
// The 2 following lines produce the expected result.
// That is the mail address passed as a parameter.
console.log("from(CL):", from);
functions.logger.log("from(FL):", from);
admin.auth().getUserByEmail(from)
.then(function(userRecord) {
我收到一条错误消息,当 运行 firebase deploy 与 getUserByEmail 阅读:
src/index.ts:37:33 - error TS2345: Argument of type 'string | string[] | ParsedQs | ParsedQs[] | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
37 admin.auth().getUserByEmail(from)
~~~~
用于处理 getUserByEmail 调用的行的正确语法是什么。
问题好像是req.body
...
同时将输入作为 query-string 传递:?from=abcd@xyz.com
.
我假设 req.query.from
应该携带一个值,而设置。
我不得不使用这个:
const from = String(req.query.from);
而不是:
const from = req.query.from;