我是Parse和Cloud Code的新手,但我已经设法编写了一些运行良好的AfterSave Cloud Code功能。但是,我在这个问题上遇到了很多麻烦,我无法弄清楚原因。请帮忙......
我有
插入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()
}
}
}
方法
问题
当我尝试检索MessageThread对象时,我尝试查询Id == threadParent.objectId。但是,此查询始终返回我当前所有8个MessageThread,而不是我需要的单个。
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...
答案 0 :(得分:2)
在整天解决了一个单独的问题后,我终于发现在Parse的Javascript SDK中,“id”和“objectId”之间存在差异。
更改此
queryM.equalTo("objectId", threadParent.id); // works!
到
module Main where
import Control.Monad (msum)
import Control.Monad.Trans.Class
import Language.Haskell.Interpreter
import Happstack.Server (nullConf, simpleHTTP, toResponse, ok, path, dir)
import Happstack.Server.Internal.Monads
import Text.JSON.Generic
import qualified Database.Redis as R
import qualified Data.ByteString.Char8 as C
main :: IO ()
main = simpleHTTP nullConf $
msum [dir "eval" $ path $ \s -> handleRequest s]
data Response = Response
{ success :: Bool
, cacheHit :: Bool
, result :: String
, error :: String
} deriving (Show, Data, Typeable)
myConnectionInfo :: R.ConnectInfo
myConnectionInfo = R.defaultConnectInfo
handleRequest :: String -> ServerPartT IO String
handleRequest exp = lift $ evalExp exp
evalExp :: String -> IO String
evalExp exp = do
cachedResult <- checkCache exp
-- CODE FOR CHECKING CACHED RESULT IS NOT YET COMPLETE
x <- runInterpreter $ setImports ["Prelude"] >> eval exp
let (error, isSuccess, result) = case x of Left _ -> ("Parse error", False, "")
Right b -> ("", True, b)
let y = Response isSuccess False result error
return $ encodeJSON y
checkCache :: String -> IO (Either R.Reply (Maybe C.ByteString))
checkCache exp = do
conn <- R.connect myConnectionInfo
R.runRedis conn $ R.get $ C.pack exp
解决了我的问题。