[英]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.