I’m playing around with ParseSwift and a local Parse Server (4.5.0). Whenever I try to perform a query or saving an object to the server I get the error below. Not sure what I’m doing wrong…
ParseError code=-1 error=Error decoding parse-server response: Optional(<NSHTTPURLResponse: 0x600003071700> { URL: http://0.0.0.0:4040//classes/Link } { Status Code: 404, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
153
);
"Content-Security-Policy" = (
"default-src 'none'"
);
"Content-Type" = (
"text/html; charset=utf-8"
);
Date = (
"Sun, 14 Mar 2021 13:20:46 GMT"
);
"Keep-Alive" = (
"timeout=5"
);
"X-Content-Type-Options" = (
nosniff
);
"X-Powered-By" = (
Express
);
} }) with error: The data couldn’t be read because it isn’t in the correct format.
My Link object:
struct Link: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var title: String?
}
This is my query:
func myQuery() {
let query = Link.query()
query.find(callbackQueue: .main) { results in
switch results {
case .success(let links):
links.forEach { link in
print("Found link: \(link)")
}
case .failure(let error):
print("Error: \(error.localizedDescription)")
}
}
}
Can you share some sample code along with the ParseObject you are trying to save/query? We would need to see your Link ParseObject and any ParseObjects it embeds.
Also, do you see errors on the server when saving/querying your object?
Can you share the code for your User ParseObject and any ParseObjects it embeds. Also, can you share how you are logging in?
When testing, you should connect your Swift Playground code (3 and 4 are relevant to your issue):
You can set the settings for your server in here:
If the playgrounds work with your server, then you most likely have something wrong in your User struct or your code and should compare it to the ones in Playgrounds.
I’ve initialized it properly since the Install data got written.
Here’s my User struct:
struct PKUser: ParseUser {
//: These are required for `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: These are required for `ParseUser`.
var username: String?
var email: String?
var password: String?
var authData: [String: [String: String]?]?
}
I’m digging into the Parse-Swift code. The server response data is:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "createdAt", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
I’ve gotten this decoding error message by adding a do {} catch {} around line 197 in ParseUser.swift. Without it, I only get the error in my first message in this thread.
Parse dates have a particular date strategy that needs to be set on the encoder/decoder, so you shouldn’t use your own. Instead, try something User().getDecoder().decode(...) if you are debugging.
I recommend setting a breakpoint at line 197 as you mentioned. In the console log winder type po try ParseCoding.jsonDecoder().decode(AnyCodable.self, from: data) and post your output. It should look similar to your server response above, but something seems weird about the dates.
Then try the original line, po try ParseCoding.jsonDecoder().decode(LoginSignupResponse.self, from: data). Does it give you an error? It may be the next line that’s giving you and issue when it attempts to decode as your user type. Try, po try ParseCoding.jsonDecoder().decode(Self.self, from: data)
In addition, try the playgrounds method I mentioned because if it works, we can rule out the SDK and the Server, leaving the issue to your implementation.
Your date doesn’t have the iso info. At that same breakpoint can you post the output for po String(data: data, encoding: .utf8)?
@davimacedo does the back4app server do anything particular with dates? The Parse-Swift SDK is expecting to see \"__type\":\"Date\",\"iso\":\"2021-03-14T18:04:20.485Z\"
Yup, the standard Parse Server dishes dates which look like: \"createdAt\":{\"__type\":\"Date\",\"iso\":\"2021-03-14T18:04:20.485Z\" which is what ParseSwift is expecting to decode.
Lets wait for @davimacedo he probably knows why this is happening and how to fix it. I suspect it’s a setting on the server side.
Just some more info on what ParseSwift expects to see, it uses the REST API (if you see something in your back4app settings that says “enable for REST”, “use REST endpoints”, or something similar, that may help). Here is the documentation that talks about the date for REST in Parse Server REST API Guide | Parse
Update: decoding the string date should still work. I added a test here:
Something is up with that particular day/time. When I use the date string you give me it fails (just like you are getting). If I switch the day or year by 1, it passes.
Maybe it has something to do with daylight savings time where I’m at? (this doesn’t make sense to me), but I’m not sure what the problem could be. Maybe something with the timezone or needing to be set to something like UTC.