简体   繁体   English

轻按按钮时解析云代码未运行

[英]Parse cloud code isn't running when button is tapped

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. 我已经设置了我的代码以在Parse后端上运行云代码,但是,点击“提交”按钮似乎无法启动该功能。 I think the issue is either in connecting the action to the submit button, or to the Parse code. 我认为问题出在将动作连接到“提交”按钮或“解析”代码上。

CriteriaViewController.m: 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');
      }

    });





});

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. 调试云代码的最佳方法是将console.log()消息放入您的云代码中,然后检查这些消息的云代码日志。 I will sometimes use console.log("step 1") and then "step 2", etc... to see where in the code it is failing. 有时我会使用console.log(“ step 1”),然后使用“ step 2”,依此类推……以查看代码在哪里失败。 I also use JSON.stringify() a lot in the console.log to see objects. 我还经常在console.log中使用JSON.stringify()来查看对象。 Debugging cloud code can be a pain. 调试云代码可能很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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