0

我製作了https雲功能,並通過iOS應用向其發送了請求。雲端函數被調用,因爲我可以看到日誌,但我無法從iOS應用發送的請求中獲取參數。我嘗試了幾種不同的選擇,但他們幫助了我,請告訴我如何才能實現結果?如何從雲功能請求中獲取參數? Firebase

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"); 
}); 

回答

6

我讀到這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"); 
}); 
相關問題