简体   繁体   English

云 function 发送 json 作为字符串

[英]Cloud function sending json as string

I started to learn cloud functions.我开始学习云函数。 What I wish to achieve is sending json to cloud function and get all documents with same phone numbers that I send in json.我希望实现的是将 json 发送到云 function 并获取所有与我在 json 中发送的电话号码相同的文档。

Cloud function:云 function:

exports.getUsers = functions.https.onRequest(async (request, response) => {
const data = request.body.data;
if (data !== null && data.users !== null) {
const users = data.users;
const phonelist = users.map(user => user.phone.toString());
const userlist = []

const snapshot = await db.collection("users").get()
snapshot.docs.forEach((userDoc) => {
    const phone = userDoc.get("phone")
    if(phone === null) return;
    const isContain = phonelist.reduce((acc, num) => acc || phone.includes(num), false)
    if(isContain) {
        userlist.push(userDoc.data())
    }
})
response.status(200).json({result: userlist})
}     else{
   response.sendStatus(403)
}
});

Ma call in Android:马云拨打Android:

  private fun addMessage(): Task<String>? {
    // Create the arguments to the callable function.
    val data = "{\n" +
            "  \"data\": {\n" +
            "    \"users\": [\n" +
            "      {\n" +
            "        \"phone\": 55512345\n" +
            "      },\n" +
            "      {\n" +
            "        \"phone\": 972525276676\n" +
            "      },\n" +
            "      {\n" +
            "        \"phone\": 55512347\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "}"

    functions.getHttpsCallable("getUsers")
        .call(data)
        .addOnFailureListener {
            Log.d("DTAG", it.toString())
        }
        .addOnSuccessListener {
            Log.d("DTAG","Ok: ${it.data.toString()}")
        }

   return null
}

I getting an error from cloud function: Cannot read properties of undefined (reading 'map')我从云 function 收到错误:无法读取未定义的属性(读取“地图”)

You shouldn't provide raw strings as JSON to the Firebase SDK. Instead, provide a Map<String, Object> if you want it to automatically convert that to a JSON Object. There is an example in the documentation .您不应将原始字符串作为 JSON 提供给 Firebase SDK。相反,如果您希望它自动将其转换为 JSON Object,请提供Map<String, Object>文档中有一个示例。 Firebase handles the JSON input and output on both ends so all you have to do is deal with native language data structures that come from the JSON, which is far more convenient and less error prone than manually building JSON. Firebase 在两端处理 JSON 输入和 output,因此您只需处理来自 JSON 的本地语言数据结构,这比手动构建 JSON 更方便且更不容易出错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 云 Function 发送 CSV 到云存储 - Cloud Function Sending CSV to Cloud Storage 通过 XMLHttpRequest() 将“content-type”作为“application/json”发送到 Google Cloud Function 导致 KeyError: 'CONTENT_TYPE' - Sending "content-type" as "application/json" via XMLHttpRequest() to a Google Cloud Function is causing a KeyError: 'CONTENT_TYPE' 发送查询字符串后,使用什么 package 和 function 来获得可以在 JSON 中格式化的响应 - What package and function to use to get a response that can be formatted in JSON after sending a query string Cloud Function 无法解析 JSON 并从 Webhook 写入 Cloud Firestore - Cloud Function failing to parse JSON and write to Cloud Firestore from Webhook 将图像作为 base64 字符串发送到 Firebase Cloud Functions 的效率 - Efficiency sending images as base64 string to Firebase Cloud Functions 如何将字符串作为输入提供给 Google Cloud Function? - How to feed a String to a Google Cloud Function as input? 如何使用 Firebase Cloud Function 加密字符串 - How to encrypt a String with a Firebase Cloud Function 通过云 Function 发送给一组收件人时,FCM 非常缓慢且不可靠 - FCM very slow and unreliable when sending to a group of recipients through Cloud Function 使用 Cloud Functions 将 Json Webhook 写入 Cloud Firestore。 云 Function 部署失败。 Function 加载用户代码失败 - Write Json Webhook to Cloud Firestore with Cloud Functions. Cloud Function Failed to Deploy. Function failed on loading user code 有什么方法可以使用云 function 和 fcm 或其他替代方法来改进快速发送推送通知? - Any way to improve sending push notification quickly using cloud function and fcm or some alternative?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM