web-dev-qa-db-ja.com

クラウド機能のリクエストからパラメーターを取得する方法は? Firebase

Httpsクラウド機能を作成し、iOSアプリでリクエストを行います。ログを見ることができるので、クラウド関数が呼び出されますが、iOSアプリから送信したリクエストからパラメーターを取得できません。私はいくつかの異なるオプションを試しましたが、それらは私を助けてくれました、結果を達成する方法を教えてください?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {
    console.log("getChatGroupConversationAvatars start", req.params);
    const requestDict = req.params;
    console.log("requestDict", requestDict);
    const queryFormat = req.query.format;
    console.log("queryFormat", queryFormat);
    const conversationID = requestDict["conversationID"];
    const currentUserID = requestDict["currentUserID"];

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});

iOSアプリコード

class ChatAvatarsManager {

    func getChatGroupConversationAvatars(_ conversationID: String) {
        DispatchQueue.main.async {
            guard let currentUserID = RealmManager().getCurrentUser()?.id else { return }
            DispatchQueue.global(qos: .background).async {
                let path = "https://us-central1-exampleapp.cloudfunctions.net/getChatGroupConversationAvatars"
                guard let url = URL(string: path) else { return }
                let parameters = ["conversationID" : conversationID, "currentUserID" : currentUserID]
                AlamofireExampleManager.shared.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
                    switch response.result {
                    case .success:
                        debugPrint("getChatGroupConversationAvatars success")
                    case .failure(let error):
                        if error._code == NSURLErrorTimedOut {
                            //timeout here
                            debugPrint("getChatGroupConversationAvatars timeOut")
                        }
                        debugPrint("getChatGroupConversationAvatars \(error)")
                    }
                }
            }
        }
    }

}

struct AlamofireExampleManager {

    static let shared: SessionManager = {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 10
        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        return sessionManager
    }()

たとえば、この結果をログに記録しました

conversationID undefined currentUserID undefined

私も試してみた

module.exports = functions.https.onRequest((req, res) => {

    const conversationID = req.params.conversationID;
    const currentUserID = req.params.currentUserID;

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});
8

これを読みます post .

const functions = require('firebase-functions');
const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {

    const conversationID = req.query.conversationID;
    const currentUserID = req.query.currentUserID;

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});
26

Webの場合:(POSTリクエスト)

我々は持っています:

export const myFunction = functions.https.onRequest((req, res) => {

    const conversationID = req.body.conversationID;
    const currentUserID = req.body.currentUserID;

 return ;
});

参照: Firestore Read From Values

4
Deepak Sharma