Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

ios - How to use multi-path update with the REST API in Firebase? Error 400

I would like to update multiple nodes in a single operation using the firebase REST API and Vapor framework. When I make .patch request to Firebase I get error from Vapor client:

The operation couldn’t be completed. Error 400 Bad Request

Could anyone tell me if finalDictionary is initialized correctly before being converted in JSON?

Desired output for Users node:

 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])
        }     
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...