如何在Heroku上运行Cloud Code?

时间:2016-02-24 10:33:49

标签: ios swift heroku cloud-code parse-server

随着Parse宣布退休,我已将我的Parse Server迁移到Heroku上。凭借我对Heroku的新手知识,我不知道它们是否具有与Cloud Code类似的功能,但我知道几个月前Parse Introduced a Heroku + Parse功能允许您在任何节点上运行Cloud Code。 js环境,特别是Heroku。

我的困境是,在学习了这个功能之前,我已经将我的服务器从解析迁移到Heroku:/,所以我不能在终端上运行任何解析云代码,因为那里没有现有的服务器了。所以问题是,如何在Heroku& amp;中模拟以下Cloud Code。我如何调整我的swift?

Cloud Code:

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:

Parse.Cloud.define("isLoginRedundant", function(request, response) {
    Parse.Cloud.useMasterKey();
    var sessionQuery = new Parse.Query(Parse.Session);
    sessionQuery.equalTo("user", request.user);
    sessionQuery.find().then(function(sessions) {
        response.success( { isRedundant: sessions.length>1 } );
    }, function(error) {
        response.error(error);
    });
});

,这是我在xcode中的快速回复:

    PFUser.logInWithUsernameInBackground(userName!, password: passWord!) {
        (user, error) -> Void in
        if (user != nil) {
            // don't do the segue until we know it's unique login
            // pass no params to the cloud in swift (not sure if [] is the way to say that)
            PFCloud.callFunctionInBackground("isLoginRedundant", withParameters: [:]) {
                (response: AnyObject?, error: NSError?) -> Void in
                let dictionary = response as! [String:Bool]
                var isRedundant : Bool
                isRedundant = dictionary["isRedundant"]!
                if (isRedundant) {
                    // I think you can adequately undo everything about the login by logging out
                    PFUser.logOutInBackgroundWithBlock() { (error: NSError?) -> Void in
                        // update the UI to say, login rejected because you're logged in elsewhere
                        // maybe do a segue here? 
                        let redundantSession: String = "you are already logged in on another device"
                        self.failedMessage(redundantSession)

                        self.activityIND.stopAnimating()

                        self.loginSecond.userInteractionEnabled = true
                    }
                } else {
                    // good login and non-redundant, do the segue 
                    self.performSegueWithIdentifier("loginSuccess", sender: self) 
                } 
            } 
        } else {
            // login failed for typical reasons, update the UI 
            dispatch_async(dispatch_get_main_queue()) {

                self.activityIND.stopAnimating()

                self.loginSecond.userInteractionEnabled = true

                if let message = error?.userInfo["error"] as? String
                    where message == "invalid login parameters" {
                        let localizedMessage = NSLocalizedString(message, comment: "Something isn't right, check the username and password fields and try again")
                        print(localizedMessage)
                        self.failedMessage(localizedMessage)
                }else if let secondMessage = error?.userInfo["error"] as? String
                    where secondMessage == "The Internet connection appears to be offline." {
                    self.failedMessage(secondMessage)
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我首先结帐example repo并阅读parse-server documentation。解析服务器支持开箱即用的云代码,您只需在parse-server配置中指定哪个文件包含您的函数和触发器。您使用parse和heroku之间的集成发布的链接与parse-server无关。