【问题标题】:parse cloud code save relation解析云代码保存关系
【发布时间】:2015-04-18 19:54:22
【问题描述】:

我正在使用 Parse 作为后端开发具有朋友关系的应用程序。当 Alice 向 Bob 发送一个好友请求时,她会生成一个类型为 70 的通知,其中她自己是 userA,Bob 是 userB。为方便起见,通知还具有 nameA 和 nameB 作为字符串。

我正在尝试在 Cloud Code 上实现 AfterSave 功能。这个函数应该将 Bob 添加到 Alice 的“潜在朋友列表”,并将 Alice 添加到 Bob 的“潜在朋友列表”。由于某些原因,我只能将 Bob 添加到 Alice 的列表,而不能添加另一个。

这是我的代码:

Parse.Cloud.afterSave("Notification", function(request, response) {

var namnamA = request.object.get('nameA');
var namnamB = request.object.get('nameB');
var tytype = request.object.get('type');
var alice = Parse.User.current();
if (tytype === 70) {
var bobQuery = new Parse.Query("User");
bobQuery.equalTo("username", namnamB);
bobQuery.limit(1);
bobQuery.find().then(function(bobs){
    bobs.forEach(function(bobby) {

        var bobbysRelation = bobby.relation("potFriendsList");
        var alicesRelation = alice.relation("potFriendsList");
        alicesRelation.add(bobby);
        alice.save().then(function(obj){
            console.log("alice has a new potential friend" + obj);
            bobbysRelation.add(alice);
            bobby.save().then(function(obj){
                console.log("bobby has a new potential friend" + obj);
            },
            function(obj, error){
                console.log(error);
            }); 
        },
        function(obj, error){
            console.log(error);
        });


    });
});
}
});

我是 JS 的新手,我已经好几个小时没能完成这项工作。非常感谢您的帮助。

【问题讨论】:

    标签: javascript parse-platform parse-cloud-code


    【解决方案1】:

    稍微清理了代码,并添加了 success() / error() 响应,我相信这就是它不起作用的原因。

    Parse.Cloud.afterSave("Notification", function(request, response) {
    
        var tytype = request.object.get('type');
        if (tytype !== 70) return response.success();
    
        var alice = Parse.User.current();
        var alicesRelation = alice.relation("potFriendsList");
    
        // EDIT - query user this way...
        var bobQuery = new Parse.Query(Parse.User);
        bobQuery.equalTo("username", request.object.get('nameB'));
    
        bobQuery.first().then(function(bob) {
            var bobsRelation = bob.relation("potFriendsList");
    
            alicesRelation.add(bob);
            bobsRelation.add(alice);
    
            return Parse.Object.saveAll([alice, bob]);
    
        }).then(function() {
            console.log("success " + arguments);
            response.success(arguments);
        }, function(error) {
            console.log("error " + error.message);
            response.error(error);
        });
    });
    

    其他一些注意事项:(0) 立即放弃非 tytype==70 案例,(1) query.first() 将只得到第一个结果,因此无需 limit(1) 和迭代,(2) @ 987654325@ 将保存一组对象,这很有用,因为您有两个。

    【讨论】:

    • 嗨@danh,非常感谢您的快速回答。尤其是你最后给我的提示。它肯定会清除我的代码。不幸的是,提供的代码仍然不起作用。我仍然可以在 Alice 的列表中记录 Bob,但反过来不行。当我只保存 [bob] 时,什么也没有发生。当我保存 [bob, alice] 而不是 [alice, bob] 时,什么也没有发生。这是我在 Parse 控制台中遇到的错误:
    • E2015-04-19T22:38:37.611Z] v121: after_save triggered for Notification for user YZ4yJaUGNW Input: { notification... } Result: TypeError: Cannot call method 'success' of undefinedat notnotification.save.success (main.js:113:17) at Parse.js:3:9956 at e (Parse.js:3:8736) at Parse.js:3:8185 at Array.forEach (native) at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661) at c.extend.resolve (Parse.js:3:8136) at e (Parse.js:3:8871) at Parse.js:3:8185 at Array.forEach (native)
    • @QuentinMalgaud - 很遗憾听到它仍然给您带来麻烦。日志消息看起来不像是由我的答案中的代码生成的,例如,我的代码没有使用forEach。您忘记进行部署了吗?
    • 另外,请参阅编辑。刚刚注意到用户查询的构造函数有点偏离...需要new Parse.Query(Parse.User)
    • 感谢您的关注@danh。我确实使用了您在另一个代码中提供的代码。现在我基本上只是复制粘贴了更新的版本,它仍然无法正常工作。同样的情况:Bob 保存在 Alice 的列表中,但不是相反。还有错误消息,包括forEach。尽管除了你给我的,什么都没有。已部署代码。这没有任何意义。就像我在查询后无法修改和保存用户一样。
    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多