为当前用户调用解析云函数 [英] Call Parse Cloud Function for current user

查看:74
本文介绍了为当前用户调用解析云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Parse中运行一些Cloud Functions,以及我在Stackoverflow上在这里找到的一个问题(

I'm trying to run some Cloud Functions in Parse, following along with a question I found here on Stackoverflow (Parse iOS SDK: Calling Cloud Functions From Xcode). Here are the functions I'd like to call within my app:

var moment = require("moment");

Parse.Cloud.define("registerActivity", function(request, response) {
    var user = request.user;
    user.set("lastActive", new Date());
    user.save().then(function (user) {
        response.success();
    }, function (error) {
        console.log(error);
        response.error(error);
    });
});

Parse.Cloud.define("getOnlineUsers", function(request, response) {
    var userQuery = new Parse.Query(Parse.User);
    var activeSince = moment().subtract("minutes", 2).toDate();
    userQuery.greaterThan("lastActive", activeSince);
    userQuery.find().then(function (users) {
        response.success(users);
    }, function (error) {
        response.error(error);
    });
});

我想在我的一个视图控制器中的viewDidLoad中调用registerActivity函数.到目前为止,这就是我所拥有的.我需要一些帮助来完成该功能:

I'd like to call the registerActivity function within a viewDidLoad within one of my view controllers. Here's what I have so far. I need some help completing the function:

override func viewDidLoad() {
        super.viewDidLoad()

        let currentUser = PFUser.currentUser()

        PFCloud.callFunctionInBackground("registerActivity", withParameters: currentUser){
            if (!error){

            }
        }

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

在"PFCloud"行上出现错误,提示:PFUser is not identical to [NSOBject:Anyobject].如何为当前用户调用此registerActivity函数?

I get an error on the "PFCloud" line that states: PFUser is not identical to [NSOBject:Anyobject]. How can I call this registerActivity function for the current user?

谢谢!

更新

将viewDidLoad中的函数调用更新为:

Updated function call in viewDidLoad to:

PFCloud.callFunctionInBackground("registerActivty", withParameters: nil, block: nil)

这会导致异常:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

最后更新: 这样就解决了问题:

LAST UPDATE: This to solved the problem:

PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")

感谢您的帮助!

推荐答案

如果currentUser已经登录,则不需要给出currentUser.在服务器端request.user将已经设置为登录用户正在提出要求.在这种情况下,您可以使用空字典的参数来调用cloud函数.

You don't need to give currentUser if it is already logged in. In server side request.user will be already set to the logged in user that is making the request. In this case, you can call the cloud function with a parameter of empty dictionary.

另一方面,您得到的错误是因为无法将PFUser对象传递给云函数.您可以将用户ID传递为["id": currentUser.objectId].然后,在服务器中,您可以作为request.params.id来访问它,以通过id获取用户.如果您想为任何用户调用云功能(即使它尚未登录).

On the other hand, the error you are getting is because PFUser object can't be passed to a cloud function. You can pass the user id as ["id": currentUser.objectId]. Then in the server, you can access it as request.params.id to get the user by id. This is in case you want to call the cloud function for any user (even it is not logged in).

这篇关于为当前用户调用解析云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆