如何从通过 HTTP 可调用函数发送的请求中获取数据?
How to get data from Request sent through HTTP callable function?
我正在使用 Firebase Admin SDK 来完成管理功能。我部署的功能位于我的控制台中的 Firebase Cloud Function 上。我已经能够从应用程序调用这些函数,但是我不知道如何从我通过调用发送的映射数据中获取值。如果我是正确的,应该能够通过 request
变量检索但是当我尝试 request.email
之类的东西时,这似乎不是一个有效的参数。
MainActivity.java
public Task<String> callCloudFunction() {
FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
Map<String, Object> data = new HashMap<>();
data.put("email", "new@example.com");
data.put("password", "changePassword");
data.put("displayName", "Mike Example");
data.put("trainerId", "Mike Example");
data.put("photoURL", "NULL");
return mFunctions
.getHttpsCallable("mkUser")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
return (String) task.getResult().getData();
}
});
}
index.ts
export const mkUser = functions.https.onRequest((request, response) =>{
admin.auth().createUser({
email: request.email, //IS NOT A VALID ARGUMENT ('email' CANNOT BE FOUND)
emailVerified: false,
password: 'secretPassword',
displayName: 'John Doe', //VALUES ARE HARDCODED BUT I WOULD LIKE TO USE 'Mike Example' SENT FROM MAIN ACTIVITY
photoURL: 'http://www.example.com/12345678/photo.png',
disabled: false
})
.then(function(userRecord: { uid: any; }) {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:', userRecord.uid);
})
.catch(function(error: any) {
console.log('Error creating new user:', error);
});
});
所以我已经弄清楚了我遇到的问题以及如何解决这个问题。
首先,在 index.ts 中,我使用的 HTTP 函数是 .onRequest
,但如果我将其更改为 .onCall
,那么我用来调用MainActivity.java 中的 HTTP 函数可以工作。
使用.onCall
将接收到的信息从'request'更改为'data';然后可以使用它来访问通过应用程序内的调用发送的映射信息。
index.ts
export const mkUser = functions.https.onCall(async (data, context) =>{
try {
const userRecord = await admin.auth().createUser({
email: data.email,
emailVerified: false,
password: data.password,
displayName: data.displayName,
disabled: false
})
console.log('Successfully created new user:', userRecord.uid);
} catch (error) {
console.log('Error creating new user:', error);
return "ERROR"
}
});
我正在使用 Firebase Admin SDK 来完成管理功能。我部署的功能位于我的控制台中的 Firebase Cloud Function 上。我已经能够从应用程序调用这些函数,但是我不知道如何从我通过调用发送的映射数据中获取值。如果我是正确的,应该能够通过 request
变量检索但是当我尝试 request.email
之类的东西时,这似乎不是一个有效的参数。
MainActivity.java
public Task<String> callCloudFunction() {
FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
Map<String, Object> data = new HashMap<>();
data.put("email", "new@example.com");
data.put("password", "changePassword");
data.put("displayName", "Mike Example");
data.put("trainerId", "Mike Example");
data.put("photoURL", "NULL");
return mFunctions
.getHttpsCallable("mkUser")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
return (String) task.getResult().getData();
}
});
}
index.ts
export const mkUser = functions.https.onRequest((request, response) =>{
admin.auth().createUser({
email: request.email, //IS NOT A VALID ARGUMENT ('email' CANNOT BE FOUND)
emailVerified: false,
password: 'secretPassword',
displayName: 'John Doe', //VALUES ARE HARDCODED BUT I WOULD LIKE TO USE 'Mike Example' SENT FROM MAIN ACTIVITY
photoURL: 'http://www.example.com/12345678/photo.png',
disabled: false
})
.then(function(userRecord: { uid: any; }) {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:', userRecord.uid);
})
.catch(function(error: any) {
console.log('Error creating new user:', error);
});
});
所以我已经弄清楚了我遇到的问题以及如何解决这个问题。
首先,在 index.ts 中,我使用的 HTTP 函数是 .onRequest
,但如果我将其更改为 .onCall
,那么我用来调用MainActivity.java 中的 HTTP 函数可以工作。
使用.onCall
将接收到的信息从'request'更改为'data';然后可以使用它来访问通过应用程序内的调用发送的映射信息。
index.ts
export const mkUser = functions.https.onCall(async (data, context) =>{
try {
const userRecord = await admin.auth().createUser({
email: data.email,
emailVerified: false,
password: data.password,
displayName: data.displayName,
disabled: false
})
console.log('Successfully created new user:', userRecord.uid);
} catch (error) {
console.log('Error creating new user:', error);
return "ERROR"
}
});