解析更新一组对象

时间:2015-07-19 09:20:38

标签: javascript parse-platform cloud-code

我在更新云代码中的一组值时遇到问题。我已经单独尝试了.save().saveAll(),但是在Parse中没有更新类,我得到了错误。

我要做的是从类ChatMessages获取所有带有指向Parse用户和Chat类的指针的消息。调用该方法时,需要将类列readAt更新为当前日期。我从iOS(Objective-C)应用程序调用我的方法。

这是我方法的最新版本:

Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
    var errorMsg;
    var roomName;

    var _ = require('underscore.js');

    var userPointer = new Parse.User.current();

    if (!request.params.roomName) {
        errorMsg = "Chat room needs to be identified";
    } else {
        roomName = request.params.roomName;
    }

    console.log("Checking chats for userID: " + userPointer.id);

    if (!userPointer.id) {
        var emptyUserMsg = "User has to be provided";
        if (errorMsg) {
            errorMsg = errorMsg + emptyUserMsg;
        } else {
            errorMsg = emptyUserMsg;
        };
    }

    if (errorMsg) {
        response.error(errorMsg);
    }

    var chatQuery = new Parse.Query("Chat");
    chatQuery.equalTo("roomName", roomName);
    chatQuery.find({
        success: function(results) {
            if (results.length > 0) {
                var chat = results[0];

                console.log("Found chat with ID: "+chat.id);

                var chatMessagesQuery = new Parse.Query("ChatMessage");
                chatMessagesQuery.equalTo("chat", chat);
                chatMessagesQuery.notEqualTo("fromUser", userPointer);
                chatMessagesQuery.equalTo("readAt", undefined);

                chatMessagesQuery.find().then(function(chatMessagesQueryResults) {
                    _.each(chatMessagesQueryResults, function(result) {
                        result.set("readAt", new Date());
                        console.log("Setting readAt for chat message " + result.id + " which has toUser " + result.get("toUser"));
                    });
                    return Parse.Object.saveAll(chatMessagesQueryResults,{
                        success: function(list) {
                           console.log("Success updating objects");
                        },
                        error: function(error) {
                           console.log("Error updating objects: " + error);
                        },});
                }).then(function(results) {
                    response.success(results);
                    console.log("Update for reatAt for chat is successfull");
                }, function(error) {
                    response.error(error);
                    console.log(error);
                });
            } else {
                response.error("No rooms found");
                console.log("No rooms found");
            }
        },
        error: function(error) {
            response.error("Room name not found");
            console.log(error);
        }
    });

});

日志输出:

E2015-07-19T09:13:48.483Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
  Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
  Result: {"code":101,"message":"object not found for update"}
I2015-07-19T09:13:48.540Z]Checking chats for userID: CZwQL4y751
I2015-07-19T09:13:48.593Z]Found chat with ID: gfvAkirqTs
I2015-07-19T09:13:48.647Z]Setting readAt for chat message ZiWUIdUtUm which has toUser undefined
I2015-07-19T09:13:48.648Z]Setting readAt for chat message YHEBLpR04U which has toUser undefined
I2015-07-19T09:13:48.649Z]Setting readAt for chat message 0wZ4LQd8ZC which has toUser undefined
I2015-07-19T09:13:48.650Z]Setting readAt for chat message MYsYGyXI0k which has toUser undefined
I2015-07-19T09:13:48.751Z]Error updating objects: [object Object]
I2015-07-19T09:13:48.752Z]{"code":101,"message":"object not found for update"}
E2015-07-19T09:13:49.042Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
  Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
  Result: {"code":101,"message":"object not found for update"}

类别:

The parse class

1 个答案:

答案 0 :(得分:3)

通过使chatMessages查询与匹配用户和房间条件的聊天相关联,可以极大地简化查询。代码结构可以通过不混合回调和promise样式,以及将逻辑块分成小的,承诺返回函数来改进。

剥离你添加的一些调试工具,我们得到(当然未经测试)......

function unreadChatMessagesInRoom(roomName, excludeUser) {
    var query = new Parse.Query("ChatMessage");
    query.notEqualTo("fromUser", excludeUser);
    query.doesNotExist("readAt");

    var chatQuery = new Parse.Query("Chat");
    chatQuery.equalTo("roomName", roomName);
    query.matchesQuery("chat", chatQuery);

    return query.find();
}

Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {    
    var _ = require('underscore.js');
    var user = request.user;

    unreadChatMessagesInRoom(request.params.roomName, user).then(function(chatMessages) {
        console.log(chatMessages.length + " chat messages found");
        _.each(chatMessages, function(chatMessage) {
            chatMessage.set("readAt", new Date());
        });
        return Parse.Object.saveAll(chatMessages);
    }).then(function(results) {
        response.success(results);
    }, function(error) {
        response.error(error);
    });
});