gpt4 book ai didi

javascript - 注册成功后在 "userCategory"下创建新的 "User"对象

转载 作者:行者123 更新时间:2023-11-29 12:47:32 26 4
gpt4 key购买 nike

当有人在我的 iOS 应用程序中注册帐户时,我想在 User 类下创建一个新的 userCategory 对象。这样,每个用户都有与其帐户关联的 userCategory 对象的多个实例。我在后端使用 Parse。

我想我可以通过让应用程序调用一个云函数来实现,该函数在注册完成时初始化 User 类下的对象,如我的 JS 代码所示。我的问题是我收到一条错误消息,指出 User is not defined at main.js:16:31。我不确定如何在 JS 中引用从 Objective-C 代码创建的类。

函数在 (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user 方法中调用。

Objective-C 代码:

#import "DefaultSettingsViewController.h"

@implementation DefaultSettingsViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (void)viewWillAppear:(BOOL)animated {
[super viewDidLoad];
if ([PFUser currentUser]) {
//[welcomeLabel setText:[NSString stringWithFormat:@"Welcome Back %@!", [[PFUser currentUser] username]]];
NSLog(@"PFUser is not current user");

} else {
NSLog(@"PFUser is current user");
//[welcomeLabel setText:@"Not logged in"];
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

if (![PFUser currentUser]) { // No user logged in
// Create the log in view controller
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self]; // Set ourselves as the delegate
[logInViewController setFacebookPermissions:[NSArray arrayWithObjects:@"friends_about_me", nil]];
[logInViewController setFields: PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten];

// Create the sign up view controller
PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
[signUpViewController setDelegate:self]; // Set ourselves as the delegate

// Assign our sign up controller to be displayed from the login controller
[logInViewController setSignUpController:signUpViewController];

// Present the log in view controller
[self presentViewController:logInViewController animated:YES completion:NULL];
} else {
[self performSegueWithIdentifier:@"login" sender:self];
}

}


#pragma mark - PFLogInViewControllerDelegate

// Sent to the delegate to determine whether the log in request should be submitted to the server.
- (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
// Check if both fields are completed
if (username && password && username.length && password.length) {
return YES; // Begin login process
}

[[[UIAlertView alloc] initWithTitle:@"Missing Information" message:@"Make sure you fill out all of the information!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
return NO; // Interrupt login process
}

// Sent to the delegate when a PFUser is logged in.
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissViewControllerAnimated:YES completion:NULL];
}

// Sent to the delegate when the log in attempt fails.
- (void)logInViewController:(PFLogInViewController *)logInController didFailToLogInWithError:(NSError *)error {
NSLog(@"Failed to log in...");
}

// Sent to the delegate when the log in screen is dismissed.
- (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController {
[self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - PFSignUpViewControllerDelegate

// Sent to the delegate to determine whether the sign up request should be submitted to the server.
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info {
BOOL informationComplete = YES;

// loop through all of the submitted data
for (id key in info) {
NSString *field = [info objectForKey:key];
if (!field || !field.length) { // check completion
informationComplete = NO;
break;
}
}

// Display an alert if a field wasn't completed
if (!informationComplete) {
[[[UIAlertView alloc] initWithTitle:@"Missing Information" message:@"Make sure you fill out all of the information!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
}

return informationComplete;
}

// Sent to the delegate when a PFUser is signed up.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user {
[self dismissViewControllerAnimated:YES completion:NULL];

[PFCloud callFunctionInBackground:@"userCategoryCreate"
withParameters:@{}
block:^(NSNumber *ratings, NSError *error) {
if (!error) {
//userCategory created
}
}];
}

// Sent to the delegate when the sign up attempt fails.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didFailToSignUpWithError:(NSError *)error {
NSLog(@"Failed to sign up...");
}

// Sent to the delegate when the sign up screen is dismissed.
- (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController {
NSLog(@"User dismissed the signUpViewController");
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

云代码 Javascript:

   // Creation of initial userCategory object

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

var userCategory = Parse.Object.extend("userCategory");

var newUserCategory = new userCategory();
newUserCategory.set("categoryId", "9355");

newUserCategory.set("parent", User);


newUserCategory.save();


});

这就是我的 Parse 仪表板的样子:

enter image description here

最佳答案

您没有告诉脚本“用户”是什么。如果您想要当前用户,请改用 Parse.User.current()

   // Creation of initial userCategory object

Parse.Cloud.define("userCategoryCreate", function(request, response) {
var userCategory = Parse.Object.extend("userCategory");
var newUserCategory = new userCategory();
newUserCategory.set("categoryId", "9355");
newUserCategory.set("parent", Parse.User.current());
newUserCategory.save();
});

关于javascript - 注册成功后在 "userCategory"下创建新的 "User"对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com