0
votes

I'm using Restkit with object mapping but I seem to be getting an error saying Encountered errors during mapping: Expected an object mapping for class of type 'Rating', provider returned one for 'Error' - now I have set up mapping and serialisation for both the Rating and Error objects, so I don't understand why it's confused. Can anyone help?

This is the mapping returned from the classes

// Rating class
+ (RKObjectMapping *)getRestKitObjectMapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Rating class]];
    [mapping mapAttributes:@"id", @"mics", @"mdate", @"ipaddress", nil];
    [mapping mapKeyPath:@"user_id" toAttribute:@"userID"];
    [mapping mapKeyPath:@"fid" toAttribute:@"fid"];


    return mapping;
}

// Error class
+ (RKObjectMapping *)getRestKitObjectMapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Error class]];
    [mapping mapKeyPath:@"code" toAttribute:@"code"];
    [mapping mapKeyPath:@"message" toAttribute:@"message"];

    return mapping;
}

This is the object routing

[[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic/:id"];
    [[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic" forMethod:RKRequestMethodPOST];

[[RKObjectManager sharedManager].mappingProvider setMapping:[Rating getRestKitObjectMapping] forKeyPath:@"ratings"];
    [[RKObjectManager sharedManager].mappingProvider setMapping:[Error getRestKitObjectMapping] forKeyPath:@"error"];

[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Rating getRestKitObjectMapping] inverseMapping] forClass:[Rating class]];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Error getRestKitObjectMapping] inverseMapping] forClass:[Error class]];

Rating *rating = [[Rating alloc] init];
[[RKObjectManager sharedManager] postObject:rating delegate:self];

These are the delegate methods, the didLoadObjects does get called

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
    NSLog(@"Object loading failed with error: %@ ----- body: %@", error.localizedDescription, objectLoader.response.bodyAsString);
}


- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    NSLog(@"Objects received: %@", objects);
    NSLog(@"Respond body: %@", objectLoader.response.bodyAsString);
}

Here is the JSON response

{"error":{"error":1,"code":342,"message":"You have already rated this one"}}

Also when I look at the $_POST value on my REST PHP backend, it's actually empty which means that it's not receiving the posting object...why is that? My routing seems ok?

1

1 Answers

0
votes

Do you have any control over your back-end services?

If so, I'd change how you're sending data back. When you're doing the "postObject" call, the only expected response back is a "Rating". I realize that you set up an "Error" mapping, but since the object you posted is a "Rating" object, it's expecting that back.

What would be more appropriate here would be for the service to change the HTTP status code to 403 (forbidden), or something that is not 200 (OK). Then you can handle the error in the "objectLoader:didFailWithError:" delegate method.