简体   繁体   中英

parse query find always returns empty result

Hello I was trying to uses Parse.com's Cloud Code API and I was trying to create a code snippet that will query the data that shows in my data browser

My cloud code is

Parse.Cloud.define("getDriver", function (request, response){
    var User = Parse.Object.extend("User");
    var query = new Parse.Query(User);
    var lastlogin;

    //response.success({"obj":request.params.objectId}); //works

    query.equalTo("objectId",request.params.objectId);
    query.find({
        success: function(objs) {

            response.success({"driver_id":objs.length});

        },
        error: function(error) {
            // The object was not retrieved successfully.
            // error is a Parse.Error with an error code and description.
        }
    });
});

I am calling the cloud code with php curl , the code is

$ch =curl_init("https://api.parse.com/1/functions/getDriver/");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($ch,CURLOPT_CAINFO,"ca-bundle.crt");

curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id:   APPLICATION_ID','X-Parse-REST-API-Key: API_KEY', 'Content-Type: application/json'));

curl_setopt($ch,CURLOPT_POSTFIELDS, "{\"objectId\": \"NDpoVvFcGP\"}");
  //execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

My problem is that the query always gives an empty result even though the correct objectId is being passed: NDpoVvFcGP

I cannot figure out this one. What might be the problem? Can someone please help?

Thanks

If you are querying the built-in User class, you should look at the documentation .

Specifically you should create your User query as follows:

var query = new Parse.Query(Parse.User);

Internally I think they use "_User" as the class name, which is why your query failed when using "User". Best to just use the documented and supported method of querying the User class.

I think your problem is with how you query for the user. Take a look at this example:

Parse.Cloud.define("totalMileage", function( request, response ) {
  var user = new Parse.User();
  user.id = request.params.userid;
  var query = new Parse.Query("Trip");
  query.include('user');
  query.equalTo("user", user);
  query.find({
    success: function(results) {
      var sum = 0;
      for ( var i = 0; i < results.length; ++i ) {
        sum +=  results[i].get("end") - results[i].get("start") ;
      }
      response.success( sum );
    },
    error: function() {
      response.error("trip lookup failed");
    }
  });
});

First I declare a user object and then I set the id with the passed in param, then I include the object in the query.

If you want more info, I have a write-up here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM