JSON 文本不是以数组或对象开头,以及允许片段的选项未在 Swift 异步函数上设置
JSON text did not start with array or object and option to allow fragments not set on Swift async function
我正在使用 Swift 和 Firebase 云函数,但无法让这个简单的调用起作用。
index.js:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
const writeResult = await admin.firestore().collection('messages').add({original: original});
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
SwiftUI 按钮:
Button {
let testVM: TestViewModel = TestViewModel()
Task.init {
try await testVM.checkTestFunc(data: "heyMama")
}
} label: {
HStack {
Image(systemName: "heart")
Text("Click me")
Spacer()
Image(systemName: "arrow.up.forward.app")
}
}
视图模型:
lazy var functions = Functions.functions()
func checkTestFunc(data: String) async throws -> Bool {
do {
let result = try await functions.httpsCallable("http://localhost:5001/myProjectID/us-central1/addMessage").call(["text": data])
print("result: \(result)")
} catch {
print("error: \(error)")
}
return false
}
错误信息:
error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0., NSJSONSerializationErrorIndex=1}
似乎有两种类型的云功能可以接收我的请求,每种都会以不同的方式获得它,因此有两种方法可以解决这个问题。
第一个是改变我发送参数的方式。因此,我不使用数组,而是将其作为函数名称的一部分发送,如下所示:
let result = try await functions.httpsCallable("addMessage?text=\(data)").call()
第二种方法是在云函数上使用 'onCall' 方法来获取数组(我更喜欢这种解决方案,因为它感觉更安全):
exports.addMessage = functions.https.onCall((data, context) => {
const original = data.text;
...
return {...};
});
我正在使用 Swift 和 Firebase 云函数,但无法让这个简单的调用起作用。
index.js:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
const writeResult = await admin.firestore().collection('messages').add({original: original});
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
SwiftUI 按钮:
Button {
let testVM: TestViewModel = TestViewModel()
Task.init {
try await testVM.checkTestFunc(data: "heyMama")
}
} label: {
HStack {
Image(systemName: "heart")
Text("Click me")
Spacer()
Image(systemName: "arrow.up.forward.app")
}
}
视图模型:
lazy var functions = Functions.functions()
func checkTestFunc(data: String) async throws -> Bool {
do {
let result = try await functions.httpsCallable("http://localhost:5001/myProjectID/us-central1/addMessage").call(["text": data])
print("result: \(result)")
} catch {
print("error: \(error)")
}
return false
}
错误信息:
error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0., NSJSONSerializationErrorIndex=1}
似乎有两种类型的云功能可以接收我的请求,每种都会以不同的方式获得它,因此有两种方法可以解决这个问题。
第一个是改变我发送参数的方式。因此,我不使用数组,而是将其作为函数名称的一部分发送,如下所示:
let result = try await functions.httpsCallable("addMessage?text=\(data)").call()
第二种方法是在云函数上使用 'onCall' 方法来获取数组(我更喜欢这种解决方案,因为它感觉更安全):
exports.addMessage = functions.https.onCall((data, context) => {
const original = data.text;
...
return {...};
});