我想使用firebase REST API和Vapor框架在单个操作中更新多个节点。
当我向Firebase发出.patch
请求时,我从Vapor客户端收到错误:
无法完成操作。错误400错误请求
在转换为JSON之前,是否可以告诉我finalDictionary
是否已正确初始化?
用户节点的所需输出:
brv-61e24
Users
usersUID
customer
bookingNumber
timeStampBookingCancelled
CleanersUID:"cleanersUID"
TimeStampBookingClaimed: "TimeStampBookingClaimed"
TimeStampBookingCancelledByCleaner: "TimeStampBookingCancelledByCleaner"
Cleaners
cleanersUID
bookingNumber
timeStampBookingCancelled
TimeStampBookingClaimed:"TimeStampBookingClaimed"
TimeStampBookingCancelledByCleaner: "TimeStampBookingCancelledByCleaner"
Cleaners
cleanersUID
bookingNumber
Claimed: "ClaimedValue"
var finalJSON:JSON?
func makeFinalDictionary() throws -> JSON? {
let customer = userStripeIdCustomer
var finalDictionary = Node([:])
//create base paths
let path0 = "Users/\(usersUID!)/\(customer!)/\(bookingNumber!)/timeStampBookingCancelled"
let path1 = "Cleaners/\(cleanersUID!)/\(bookingNumber!)/timeStampBookingCancelled"
let path2 = "Cleaners/\(cleanersUID!)/\(bookingNumber!)"
//create final paths and assign values to keys
finalDictionary[path0] = try Node(node: ["CleanersUID": cleanersUID])
finalDictionary[path0] = try Node(node:["TimeStampBookingClaimed":timeStampBookingClaimed])
finalDictionary[path0] = try Node(node:["TimeStampBookingCancelledByCleaner":timeStampBookingCancelledByCleaner])
finalDictionary[path1] = try Node(node:["TimeStampBookingClaimed":timeStampBookingClaimed])
finalDictionary[path1] = try Node(node:["TimeStampBookingCancelledByCleaner":timeStampBookingCancelledByCleaner])
finalDictionary[path2] = try Node(node:["Claimed":claimedValue])
do {
finalJSON = try JSON(Node(node:finalDictionary))
} catch let error as NSError {
let message = error.localizedDescription
drop.log.error(message)
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : message])
}
return finalJSON
}
func writeAsMultiPath(tokenReceived:String!, jsonObject:JSON?) throws -> Void {
let responseFirebase:Response?
do{
let token = tokenReceived ?? "defaultValue"
let response = try drop.client.patch(
"https://brv-61e24.firebaseio.com.json/", headers: ["Authorization":"Bearer \(tokenReceived)"], query: [:],
body: jsonObject!)
responseFirebase = response
}catch let error as NSError {
guard let phrase = error.userInfo["error"] as? String else {
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : error.localizedDescription])
}
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : phrase])
}
let body = responseFirebase?.body.bytes
do {
jsonFromWritingUsers = try JSON(bytes:body!)
} catch let error as NSError{
let message = "jsonFromWritingUsers cannot be converted in JSON line 408 in\(fileName)"
drop.log.error(message)
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : message])
}
guard responseFirebase?.status.statusCode == 200 else{
let message1 = "write as multi-path failed line 410 in\(fileName)"
drop.log.error(message1)
let message = "fail"
throw NSError(domain: "errorFirebase", code: 400, userInfo: ["error" : message])
}
}