javascript - 在 iOS 应用程序中更新 PFObject 的 key 时,需要在从 Parse Cloud Code 保存和登录时运行代码

标签 javascript ios parse-platform

我有一个具有数组键的 PFObject。我可以在此 PFObject 上成功调用 addObject:,并且可以使用 NSLog 确认该对象已正确添加到数组键中。然而,当我尝试将 PFObject 保存到 Parse 时,尽管它说一切都成功了,但数据浏览器中没有显示更改。

我已经尝试了一切,甚至可以让它在我的应用程序的旧版本中工作,但由于某种原因它不再工作了。

我发布了另一个关于这个 here 的 StackOverflow 问题

我得到的唯一回应是一些评论说我应该触发“保存前”功能并通过 Cloud Code 记录所有内容。问题是我不懂 javascript,我一直在摆弄 Cloud Code 但什么也没发生。

这是我在我的应用程序中执行的代码:

[self.message addObject:currentUsersObjectId forKey:@"myArrayKey"];

然后我使用 saveInBackgroundWithBlock:

我需要更改 Cloud Code,以便它在保存和记录结果之前检查 self.message 对象的 "myArrayKey"

编辑 2:

这是我创建 currentUsersObjectId 的方式:

NSString *currentUsersObjectId = [[NSString alloc]init];

PFUser *user = [PFUser currentUser];

currentUsersObjectId = user.objectId;

编辑 3:

这是保存 block

[self.message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error) {
        NSLog(@"An error has occurred.");
    } 
}];

编辑 4:

添加 Timothy 的云代码后,saveInBackgroundWithBlock: 现在没有成功完成。相反,会发生错误,并且错误对象 NSLogs 为“错误:未捕获试图用指向新的未保存对象的指针保存对象。(代码:141,版本:1.2.17)”以及:

Error Domain=Parse Code=141 "The operation couldn’t be completed. (Parse error 141.)" UserInfo=0x15dc4550 {code=141, error=Uncaught Tried to save an object with a pointer to a new, unsaved object.} {
    code = 141;
    error = "Uncaught Tried to save an object with a pointer to a new, unsaved object.";
}

这是我添加 Timothy 代码后的完整 Cloud Code 文件:

Parse.Cloud.define('editUser', function(request, response) {
    var userId = request.params.userId;
        //newColText = request.params.newColText;

    var User = Parse.Object.extend('_User'),
        user = new User({ objectId: userId });

    var currentUser = request.user;

    var relation = user.relation("friendsRelation");
    relation.add(currentUser);

    Parse.Cloud.useMasterKey();
    user.save().then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });
});

Parse.Cloud.beforeSave("Messages", function(request, response) {
    var message = request.object;

    // output just the ID so we can check it in the Data Browser
    console.log("Saving message with ID:", message.id);
    // output the whole object so we can see all the details including "didRespond"
    console.log(message);

    response.success();
});

// log the after-save too, to confirm it was saved
Parse.Cloud.afterSave("Messages", function(request, response) {
    var message = request.object;

    // output just the ID so we can check it in the Data Browser
    console.log("Saved message with ID:", message.id);
    // output the whole object so we can see all the details including "didRespond"
    console.log(message);

    response.success();
});

最佳答案

来回反复之后,我很困惑为什么这对您不起作用。至于登录 Cloud Code,如果您按照添加代码指南进行操作,则可以将以下内容添加到您的 main.js 并进行部署:

Parse.Cloud.beforeSave("Messages", function(request, response) {
    var message = request.object;

    // output just the ID so we can check it in the Data Browser
    console.log("Saving message with ID:", message.id);
    // output the whole object so we can see all the details including "didRespond"
    console.log(message);

    response.success();
});

// log the after-save too, to confirm it was saved
Parse.Cloud.afterSave("Messages", function(request, response) {
    var message = request.object;

    // output just the ID so we can check it in the Data Browser
    console.log("Saved message with ID:", message.id);
    // output the whole object so we can see all the details including "didRespond"
    console.log(message);

    response.success();
});

有了这些,您就有了大量可以检查的服务器端日志记录。

关于javascript - 在 iOS 应用程序中更新 PFObject 的 key 时,需要在从 Parse Cloud Code 保存和登录时运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748104/

相关文章:

ios - 在Xcode5 SDK 3.03中进行编译时添加Google Analytics(分析)链接器错误

ios - 使用 MPMoviePlayerController 播放录制的视频时 iOS 7 中的 _itemFailedToPlayToEnd 问题

java - 解析 ListView 中的 ParseQueryAdapter 和多对多关系 [Parse.com]

javascript - 在 Highcharts 中,是否可以为图例符号添加边框?

javascript - 使用 Gmail Apps 脚本以 HTML 和普通格式发送电子邮件

php - 在 Web 应用程序中使用 Google Maps API

javascript - 如何更新 cordova.js?

ios - GL_TRIANGLE_STRIP 在顶点末端绘制回原点

Javascript with Parse - 使用异步循环保存

ios - var newUser = PFUser() 它的操作似乎不起作用?