我有一个相对简单的 Firebase 函数,它只是创建一个对 API 的 https 请求,然后应该对数据库进行几次写入。然而,即使它成功完成了 API 调用(我可以在服务的调试器中看到),它总是导致错误:无法处理请求。
功能
export const CreateNoonlightAlarm = functions.https.onRequest((request, response) => {
const options = {
host: noonlightURL,
path: "/dispatch/v1/alarms",
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${noonlightToken}`,
},
};
var req = https.request(options, (res) => {
res.on("data", (response) => {
let data = JSON.parse(response);
const docRef = database
.collection("systems")
.doc(request.body.owner_id)
.collection("alarms")
.doc(data.id);
docRef
.set(data, { merge: true })
.catch((error: any) => console.log("docRef:", error));
const referenceRef = database
.collection("alarmsToSystems")
.doc(data.id);
referenceRef
.set({
alarm_id: data.id,
system_id: request.body.owner_id,
})
.catch((error: any) => console.log("referenceRef:", error));
response.send(response);
});
});
req.on("error", (e) => {
response.send(e);
});
req.write(JSON.stringify(request.body));
req.end();
}
);
现在,我已经有一段时间了。我已尝试删除 res.on("data", (response) => {}
块中除 response.send(response);
之外的所有内容,但仍然收到错误消息。我还看到很多人在 Firebase 实际更新函数时遇到了问题,并说在运行 firebase init
之前再次运行 firebase deploy
,但这对我来说仍然不起作用。我还控制台记录了 request.body.owner_id
和 data.id
之类的内容,以确保它们可读,而且确实如此。同一文件中的其他函数在调用时运行得非常好,并返回它们预期返回的内容。
我是否遗漏了什么让这个函数成功运行?
答案 0 :(得分:0)
所以,在又一个小时的故障排除和一行一行的挖掘之后,我意识到了一些事情。你不能在同一个函数中将两个事物命名为相同的事物......结果永远不会很好。
在第 1 行中,我将 var config = PHPickerConfiguration()
config.selectionLimit = 1
config.filter = PHPickerFilter.any(of: [.images, .livePhotos])
let picker_Photo = PHPickerViewController(configuration: config)
picker_Photo.delegate = self
let libCell = UIAction(title: "Photo Library", image: UIImage(systemName: "photo"), identifier: .none, discoverabilityTitle: .none) { (libAction) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
{
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
switch status
{
case .limited:
DispatchQueue.main.async
{
// PHPhotoLibrary.shared().register(self)
// PHPhotoLibrary.shared().presentLimitedLibraryPicker
self.present(picker_Photo, animated: true, completion: nil)
}
case .authorized:
DispatchQueue.main.async
{
self.present(picker_Photo, animated: true, completion: nil)
}
case .notDetermined:
DispatchQueue.main.async
{
self.present(picker_Photo, animated: true, completion: nil)
}
case .restricted:
self.view.sendConfirmationAlert(theTitle: "Photo Library Restricted",
theMessage: "Photo Library access was previously denied. Please update your Settings to allow access.", buttonTitle: "Ok")
case .denied:
let settingsAlert = UIAlertController(title: "Photo Library Access Denied",
message: "Photo Library access was previously denied. Please update your Settings to allow access.", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Go to Settings", style: .default) { ( action ) in
let settingsUrl = URL(string: UIApplication.openSettingsURLString)
UIApplication.shared.open(settingsUrl!, options: [:])
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
DispatchQueue.main.async
{
settingsAlert.addAction(settingsAction)
settingsAlert.addAction(cancelAction)
self.present(settingsAlert, animated: true)
}
default:
return
}
}
}
}
中的第二个参数定义为 OnRequest()
。
在第 14 行,我再次将 response
定义为 response
中的参数。
因此,当我将第 14 行的 res.on()
更改为 response
,然后每次相应地使用该变量时,该函数开始完全按照预期工作。更新后的代码可以在下面找到。
res