gpt4 book ai didi

javascript - 解析更新一组对象

转载 作者:行者123 更新时间:2023-11-29 10:39:36 25 4
gpt4 key购买 nike

我在更新云代码中的一组值时遇到问题。我已经分别尝试了 .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

最佳答案

通过使 chatMessages 查询与符合用户和房间条件的聊天相关,可以大大简化查询。代码结构可以通过不混合回调和 promise 样式,并将逻辑 block 分成小的、返回 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);
});
});

关于javascript - 解析更新一组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499715/

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