I'm having the error 3840 "JSON text did not start with array or object and option to allow fragments not set." on my application after being on the same page for a while.
I'm calling a cloud code function from my Parse server, getting the answer and displaying it. It works fine, but after waiting a little bit the application crashes.
I'm calling my manager for the cloud code function:
ParseManager.sharedManager().fetchPrice(startDate: startDate, endDate: endDate, housingId: housing.objectId!) { (objects, error) in
if (objects != []) {
var price = 0
for object in objects! {
price += object["prix"] as! Int
}
price /= objects!.count
self.priceByNight.text = "Price: \(price)"
}
}
In my manager:
func fetchPrice(startDate: Date, endDate: Date, housingId: String, completion: PFResults?) {
let params = [
"startDate": startDate,
"endDate": endDate,
"housing": housingId
] as [String : Any]
PFCloud.callFunction(inBackground: "fetchPrice", withParameters: params) { (objects, error) in
if let error = error {
print(error)
completion?(nil, error)
return
}
if let objects = objects as? [PFObject] {
completion?(objects, nil)
}
}
}
This is what my function send me back:
{
"result": [{
"price": 10,
"createdAt": "2019-05-07T12:39:47.320Z",
"updatedAt": "2019-05-09T15:31:25.957Z",
"date": {
"__type": "Date",
"iso": "2019-05-20T12:00:00.000Z"
},
"idHousing": {
"__type": "Relation",
"className": "myOtherClassName"
},
"objectId": "XXXXXXXXXX",
"__type": "Object",
"className": "MyClassName"
}]
}
The problem was how I was checking what the manager was returning, checking it the wrong way made it try to do things with a null object.
The solution was to change if (objects != []) {
to if let objs = objects {
and it works now.