*Simple* Parse Cloud Code query failure “TypeError: Cannot call method 'get' of undefined\n retrieve that info ”

南楼画角 提交于 2019-12-25 04:19:28

问题


I've been trying to do something very simple for several hours now and I don't know what I'm doing wrong. I'm simple trying to query the username of the first person from my Parse data set via Cloud Code, and then I want to bring that down to my iOS application. Despite all attempts it doesn't seem to be working. Below you'll find my code.

Parse.Cloud.define("userName", function(request,response){

    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.username)
    query.first({
        success: function(getUserName) {
            var userString = getUserName.get("username");
            response.success(userString);
        },
        error: function(error) {
            alert("Error: " + error.code + " " + error.message);
        }
        });
    });





[PFCloud callFunctionInBackground:@"userName" withParameters:@{} block:^(NSString *result, NSError *error) {
        if (!error){
            NSLog(result);
        }
    }];

I should also not that I'm bot sure what to put in the Parameters section besides "username :"

EDIT: Further, when I try to deploy the Parse Cloud Code I get "TypeError: Cannot call method 'get' of undefined\n"


回答1:


Looking at your cloud code, you're expecting a request.params.username to contain a value that you can use to lookup in the User class, via the username column. I typically access attributes of the params object via a key (I'm not sure if this is required, I just know it works for me). Try changing

query.equalTo("username", request.params.username)

to

query.equalTo("username", request.params["username"]);

Then, you need to get that data into the cloud function. Your Objective-C code passes an empty parameter block in, so looking anything up there will result in a whole lot of nothing. Change your PFCloud call to

[PFCloud callFunctionInBackground:@"userName" withParameters:@{@"username": @"theusernameyouwanttolookup"} block:^(NSString *result, NSError *error) {

And hopefully you'll have some better luck.




回答2:


Figured it out. My mistakes came from a poor understanding of Javascript.

The below cloud code got what I wanted.

Parse.Cloud.define("userName", function(request,response){
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.first({
    success: function(object) {
        var userString = request.params.username;
        response.success(userString);
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
    });
});

The mistake I was making was that I was trying to use the ".get" method on an object that didn't have access to the said method. The weird thing is I tried to do the same thing with request.object.get("username") but apparently the request object also doesn't have access to the .get method (confusing because the Parse documentation seems to indicate that it does in its examples). So doing the above in combination with rickerbh's objective-c code below got me the result I was looking for.

It would help if someone could post the nature of the .get method in relation to Parse's cloud code here. Cheers.



来源:https://stackoverflow.com/questions/28823188/simple-parse-cloud-code-query-failure-typeerror-cannot-call-method-get-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!