Node.js 如何从通过HTTP可调用函数发送的请求中获取数据?
我正在使用Firebase Admin SDK完成管理功能。我已部署的功能位于控制台中的Firebase Cloud功能上。我已经能够从应用程序中调用这些函数,但是我不知道如何从通过调用发送的映射数据中获取值。如果我是正确的,那么应该能够通过Node.js 如何从通过HTTP可调用函数发送的请求中获取数据?,node.js,typescript,httprequest,android-json,Node.js,Typescript,Httprequest,Android Json,我正在使用Firebase Admin SDK完成管理功能。我已部署的功能位于控制台中的Firebase Cloud功能上。我已经能够从应用程序中调用这些函数,但是我不知道如何从通过调用发送的映射数据中获取值。如果我是正确的,那么应该能够通过request变量检索,但是当我尝试类似request.email之类的操作时,这似乎不是一个有效的参数 MainActivity.java public Task<String> callCloudFunction() {
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中,我使用的HTTP函数是
.onRequest
,但是如果我将其更改为.onCall
,那么我用来在MainActivity.java中调用HTTP函数的代码就可以工作了
使用.onCall
将收到的信息从“请求”更改为“数据”;然后可以使用它访问通过应用程序内的调用发送的映射信息
索引
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);
});
});
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"
}
});