我是Parse和Cloud Code的新手,但是我设法编写了一些可以正常工作的AfterSave Cloud Code函数。但是,我对此有很多麻烦,我不知道为什么。请帮忙...

我有


两种PFObject类:Message和MessageThread
消息包含与MessageThread相关的聊天消息
MessageThread包含一个成员数组(都是PFUsers)
在插入Message时,我想查找相关MessageThread的所有成员,并向他们推送通知

class MessageThread: PFObject {

    @NSManaged var members: [PFUser]
    @NSManaged var lastMessageDate: NSDate?
    @NSManaged var messageCount: NSNumber?

    override class func query() -> PFQuery? {
        let query = PFQuery(className: MessageThread.parseClassName())
        query.includeKey("members")
        return query
    }
    init(members: [PFUser], lastMessageDate: NSDate?, messageCount: NSNumber?) {
        super.init()

        self.members = members
        self.lastMessageDate = lastMessageDate
        self.messageCount = messageCount
    }

    override init() {
        super.init()
    }
}
extension MessageThread: PFSubclassing {
    class func parseClassName() -> String {
        return "MessageThread"
    }
    override class func initialize() {
        var onceToken: dispatch_once_t = 0
        dispatch_once(&onceToken) {
            self.registerSubclass()
        }
    }
}
class Message: PFObject {
    @NSManaged var messageThreadParent: MessageThread
    @NSManaged var from: PFUser
    @NSManaged var message: String
    @NSManaged var image: PFFile?

    override class func query() -> PFQuery? {
        let query = PFQuery(className: Message.parseClassName())
        query.includeKey("messageThreadParent")
        return query
    }
    init( messageThreadParent: MessageThread, from: PFUser, message: String, image: PFFile?) {
        super.init()

        self.messageThreadParent = messageThreadParent
        self.from = from
        self.message = message
        self.image = image
        }
    override init() {
        super.init()
    }
}
extension Message: PFSubclassing {
    class func parseClassName() -> String {
        return "Message"
    }
    override class func initialize() {
        var onceToken: dispatch_once_t = 0
        dispatch_once(&onceToken) {
            self.registerSubclass()
        }
    }
}



方法


从请求对象(消息)中获取其messageThreadParent
查找父MessageThread的成员,循环遍历,等等。


问题


当我尝试检索MessageThread对象时,尝试在Id == threadParent.objectId上进行查询。但是,此查询始终返回我当前的所有8个MessageThreads,而不是我需要的单个。

Parse.Cloud.afterSave(Parse.Object.extend("Message"), function(request) {

Parse.Cloud.useMasterKey();

var theMsg = request.object;
var threadParent;
var currUsername = request.user.get("username");
var threadUsers;
var usernameArray;

threadParent = request.object.get("messageThreadParent");

// promise
queryM = new Parse.Query(Parse.Object.extend("MessageThread"));
queryM.include("members");
queryM.equalTo("id", threadParent.objectId);
queryM.find().then(function (threadParam) {

console.log(" threads: ");
console.log(threadParam.length); //this returns 8, which is the number of threads I have.  I would expect just 1, matching threadParent.objectId...
console.log("thread is: ");

//... additional code follows, which seems to work...

最佳答案

经过一整天的努力,我终于发现在Parse的Javascript SDK中,“ id”和“ objectId”之间是有区别的。

改变这个

 queryM.equalTo("id", threadParent.objectId); // doesn't work




 queryM.equalTo("objectId", threadParent.id); // works!


解决了我的问题。

09-25 18:26