Firebase 函数查询参数为空
Firebase functions query parameters are null
我在将查询参数传递给我的 firebase(google 云)函数时遇到困难,它们一直为空,但函数正常完成。谁能找出问题所在?
云函数代码
exports.message = functions.https.onRequest((request, response) => {
functions.logger.log("log query");
functions.logger.log(request.query);
response.json({result: `done`});
});
本地应用代码:
export const sendPushNotification = async (options) => {
try {
const db = firebase.firestore();
const fbFunctions = firebase.functions();
const message = fbFunctions.httpsCallable("message");
message({text:"asdfasdf"})
} catch (error) {
console.log({ error });
switch (error.code) {
default:
return {
error: "Error.",
};
}
}
};
无法使用 Firebase Functions SDK 调用 onRequest 类型的函数。 Firebase SDK 实现了 callable function that you declare with onCall. You're using onRequest here, which means you're writing a standard HTTP type function. For this type of function, you should use a standard HTTP client (not the Firebase SDK). If you actually did want to use the Firebase SDK to invoke your function, you will have to write a callable function instead. Note that callable functions have their own spec 的客户端,您将无法轻松调用它们,例如来自邮递员。
使用functions.https.onCall 创建一个 HTTPS 可调用函数。此方法有两个参数:数据和可选的上下文:
这是适合您的代码:
exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
if (!(typeof text === 'string') || text.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
text: sanitizedMessage,
author: { uid, name, picture, email },
}).then(() => {
console.log('New Message written');
return { text: sanitizedMessage };
})
.catch((error) => {
throw new functions.https.HttpsError('unknown', error.message, error);
});
});
按照以下步骤设置客户端开发环境:
- Add Firebase to your Web App.
- 将 Firebase 核心和 Cloud Functions 客户端库添加到您的应用程序:
3. 运行 npm install firebase@8.10.0 --保存
4. 手动要求 Firebase 核心和 Cloud Functions:
const firebase = require("firebase");
// Required for side-effects
require("firebase/functions");
- 初始化客户端 SDK 使用:
- 调用函数使用:
我在将查询参数传递给我的 firebase(google 云)函数时遇到困难,它们一直为空,但函数正常完成。谁能找出问题所在?
云函数代码
exports.message = functions.https.onRequest((request, response) => {
functions.logger.log("log query");
functions.logger.log(request.query);
response.json({result: `done`});
});
本地应用代码:
export const sendPushNotification = async (options) => {
try {
const db = firebase.firestore();
const fbFunctions = firebase.functions();
const message = fbFunctions.httpsCallable("message");
message({text:"asdfasdf"})
} catch (error) {
console.log({ error });
switch (error.code) {
default:
return {
error: "Error.",
};
}
}
};
无法使用 Firebase Functions SDK 调用 onRequest 类型的函数。 Firebase SDK 实现了 callable function that you declare with onCall. You're using onRequest here, which means you're writing a standard HTTP type function. For this type of function, you should use a standard HTTP client (not the Firebase SDK). If you actually did want to use the Firebase SDK to invoke your function, you will have to write a callable function instead. Note that callable functions have their own spec 的客户端,您将无法轻松调用它们,例如来自邮递员。
使用functions.https.onCall 创建一个 HTTPS 可调用函数。此方法有两个参数:数据和可选的上下文: 这是适合您的代码:
exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
if (!(typeof text === 'string') || text.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
text: sanitizedMessage,
author: { uid, name, picture, email },
}).then(() => {
console.log('New Message written');
return { text: sanitizedMessage };
})
.catch((error) => {
throw new functions.https.HttpsError('unknown', error.message, error);
});
});
按照以下步骤设置客户端开发环境:
- Add Firebase to your Web App.
- 将 Firebase 核心和 Cloud Functions 客户端库添加到您的应用程序:
4. 手动要求 Firebase 核心和 Cloud Functions:
const firebase = require("firebase");
// Required for side-effects
require("firebase/functions");
- 初始化客户端 SDK 使用:
- 调用函数使用: