Question

I have my code set up to run cloud code on Parse backend, however, tapping the submit button doesn't seem to initiate the function. I think the issue is either in connecting the action to the submit button, or to the Parse code.

CriteriaViewController.m:

#import "CriteriaViewController.h"

@interface CriteriaViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemConditionSegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemLocationSegment;

@property (weak, nonatomic) IBOutlet UIButton *submitButton;


@end

@implementation CriteriaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];

}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)itemConditionSegment:(id)sender{


    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if (selectedSegment == 0) {

        self.itemCondition = @"new";

    }
    else{
        self.itemCondition = @"any";
    }

}

- (IBAction)itemLocationSegment:(id)sender {



    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if (selectedSegment == 0) {

        self.itemLocation = @"US";

    }
    else if (selectedSegment == 1) {

        self.itemLocation = @"WorldWide";

    }


}



//add all the info to users respective new category object
- (IBAction)submitButton:(id)sender
{
    if (self.minPrice.text.length > 0 && self.maxPrice.text.length > 0)

    {

        [PFCloud callFunctionInBackground:@"userCategorySave"
                           withParameters:@{@"categoryId": self.chosenCategory,
                                              @"minPrice": self.minPrice.text,
                                              @"maxPrice": self.maxPrice.text,
                                         @"itemCondition": self.itemCondition,
                                          @"itemLocation": self.itemLocation,}
                                         block:^(NSString *result, NSError *error) {

                                             if (!error) {
                                                 NSLog(@"Criteria successfuly saved.");

                                                     [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];

                                             }
                                         }];

}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

}


@end

Cloud code function:

Parse.Cloud.define("userCategorySave", function(request, response) {







var newUserCategory = new userCategory();
    newUserCategory.set("categoryId", request.params.categoryId);
    newUserCategory.set("minPrice", request.params.minPrice);
    newUserCategory.set("maxPrice", request.params.maxPrice);
    newUserCategory.set("itemCondition", request.params.itemCondition);
    newUserCategory.set("itemLocation", request.params.itemLocation);
    newUserCategory.set("parent", Parse.User.current());

    newUserCategory.save({ 

      success: function (){
        console.log ('userCategory successfully created!');
        response.success('Request successful');
      },

      error: function (){
        console.log('error!!!');
      response.error('Request failed');
      }

    });





});
Was it helpful?

Solution

It looks like you aren't initiating your Parse Object correctly. It should be done like this:

var UserCategory = Parse.Object.extend("userCategory")
var newUserCategory = new UserCategory()
...

Did you check the cloud code logs for error messages? The best way to debug cloud code is to put console.log() messages in your cloud code and then check the cloud code logs for those messages. I will sometimes use console.log("step 1") and then "step 2", etc... to see where in the code it is failing. I also use JSON.stringify() a lot in the console.log to see objects. Debugging cloud code can be a pain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top