简体   繁体   中英

How to return an array from a Parse CloudCode Query?

I am trying to call a Parse CloudCode function and receive an array in response. Unfortunately my iOS apps crashes. It think I am not correctly matching the return type from Parse. Unfortunately my knowledge of the force - I mean Java - is limited.

iOS Code:

[PFCloud callFunctionInBackground:@"storyReadCount"
                   withParameters:@{}
                            block:^(NSArray *results, NSError *er)
 {
     if (er) {
         NSLog(@"the error is %@", er);
     }else{
        if (results.count > 0) {
             //iterate through results adding story objects to array
            for (int lCounter = 0; lCounter < results.count; lCounter ++) {
                PFObject *singleObject = [results objectAtIndex: lCounter];
                RSSStory *singleStory = [[RSSStory alloc] init];
                singleStory.link = [singleObject objectForKey: @"link"];
                singleStory.readCount = [singleObject objectForKey: @"count"];
                singleStory.loveCount = [singleObject objectForKey: @"heart"];
                [array addObject: singleStory];
                                        }
            _totalStoryReadCount = array;
            [[NSNotificationCenter defaultCenter] postNotificationName: @"PriorStoryCountLoaded" object: nil];
            }
     }
 }
 ];

Parse CloudCode:

        //gets and returns array containing number of times each article has been read
Parse.Cloud.define("storyReadCount", function(request, response) {
                    var query = new Parse.Query("Story");
                    query.descending("createdAt");
                    query.exists("link");
                    query.limit (100);
                    query.find({
                              success: function(results) {
                              response.success(results);
                              },
                              error: function(error) {
                              response.error("Failed");

                               }
                              })
                              }
                   );

I swear It was working properly for a day. I commented out if(err)else block to verify there was no logic error, but the crash occurs before that.

Crash Error Message seems to vary, but is some variation of: __NSDate UTF8String]: unrecognized selector sent to instance. I have also seen _NSTaggedDate in crash log on occasion.

I have also tried changing:

block:^(NSArray *results, NSError *er)

to

block:^(NSString *results, NSError *er)

and

block:^(PFObject *results, NSError *er)

and for fun I tried:

block:^(NSDate *results, NSError *er)

Here's how to return results from a cloud query, using promises...

Parse.Cloud.define("storyReadCount", function(request, response) {
    var query = new Parse.Query("Story");
    query.descending("createdAt");
    query.exists("link");
    query.limit (100);
    query.find().then(function(results) {
        response.success(results);
    }, function(error) {
        response.error(error);
    });
});

Here is what solved my problem:

To my cloudCode Query add:

        query.include("UserID");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM