PFQuery.find objectId 未在 Parse JavaScript SDK(云代码)中返回唯一行
PFQuery.find on objectId is not returning a unique row in Parse JavaScript SDK (Cloud Code)
我是 Parse 和 Cloud Code 的新手,但我已经设法编写了一些运行良好的 AfterSave Cloud Code 函数。但是,我在这方面遇到了很多麻烦,而且我不明白为什么。请帮忙...
我有
- 两个 PFObject 类:Message 和 MessageThread
- 消息包含与 MessageThread 关联的聊天消息
- MessageThread 包含一个成员数组(都是 PFUser)
插入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()
}
}
}
方法
- 从请求对象(一个Message)中获取它的messageThreadParent
- 查找父 MessageThread 的成员,遍历它们等
问题
当我尝试检索 MessageThread 对象时,我尝试查询 Id == threadParent.objectId。但是,此查询总是 returns 我当前的所有 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!
解决了我的问题。
我是 Parse 和 Cloud Code 的新手,但我已经设法编写了一些运行良好的 AfterSave Cloud Code 函数。但是,我在这方面遇到了很多麻烦,而且我不明白为什么。请帮忙...
我有
- 两个 PFObject 类:Message 和 MessageThread
- 消息包含与 MessageThread 关联的聊天消息
- MessageThread 包含一个成员数组(都是 PFUser)
插入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() } } }
方法
- 从请求对象(一个Message)中获取它的messageThreadParent
- 查找父 MessageThread 的成员,遍历它们等
问题
当我尝试检索 MessageThread 对象时,我尝试查询 Id == threadParent.objectId。但是,此查询总是 returns 我当前的所有 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!
解决了我的问题。