Parse Query using Geoquery - parse-platform

I'm running queries on some objects with linked geoquery data.
However, none of the queries work and return the following error
location object expected, location array not in correct format
Console logs show that
gp is definitely an object, is definitely a Parse Geopoint and has correct coordinates.
I'm stumped, any advice appreciated
let gp = null;
if (search.hasOwnProperty("geo")) {
console.log(`Geodata ${JSON.stringify(search.geo)}`);
gp = new Parse.GeoPoint({
latitude: search.geo.lat,
longitude: search.geo.long
});
console.log(`GP type ${typeof gp}, GP ${JSON.stringify(gp)}`);
let query = new Parse.Query("Place");
query.near("location", gp);
query.find({
success: (e) => console.log(`Geo near query ${JSON.stringify(e)}`),
error: (e) => console.log(`Geo near error ${JSON.stringify(e)}`)
});
};

I created something on my side that work with the following code:
let gp = new Parse.GeoPoint({
latitude: 37.337845,
longitude: -122.037687
});
// 1. first run this in order to save some location to your DB
var LocationTest = Parse.Object.extend("LocationTest");
var locationObject = new LocationTest();
locationObject.set("location",gp);
locationObject.set("name","location name test");
locationObject.save().then(function(result){
},function(error){
});
// 2. Uncomment and run this query after you save the object above
/*
var query = new Parse.Query("LocationTest");
query.near("location",gp);
query.find().then(function(results){
},function(error){
}); */
Please first run the first piece of code in order to save a location to your DB and then run the second part for query what you have saved.
The reason that you are getting this error can be because your location is not saved as an object but as an array on your database or maybe because your location field is not of type PFGeoPoint
what i suggest you is maybe to first run my code above and check if it works and then maybe try to change maybe the class name or the field name in your DB and save and query from a different field.
This code works for me on parse-server 2.2.17 (the latest version) so please also check that your parse-server is up to date by running npm install on your app

With some experimentation, found that geoqueries fail if any of the objects being queried don't have geodata associated with it.
Bit more severe than just not having geodata, even if it has a geodata property, and that property is empty, it will crash.
Solution is to check that latitude and longitude exist before querying that object

Related

Getting Parse Objects via pointers

I am trying to get a Reservation object which contains a pointer to Restaurant.
In Parse Cloud code, i am able to get the restaurants objects associated with Reservations via query.include('Restaurant') in log just before response.success. However, the Restaurants reverted back to pointer when i receive the response on client app.
I tried reverted JSSDK version to 1.4.2 & 1.6.7 as suggested in some answers, but it doesn't work for me.
Parse.Cloud.define('getreservationsforuser', function(request, response) {
var user = request.user
console.log(user)
var query = new Parse.Query('Reservations')
query.equalTo('User', user)
query.include('Restaurant')
query.find({
success : function(results) {
console.log(JSON.stringify(results))
response.success(results)
},
error : function (error) {
response.error(error)
}
})
})
response :
..."restaurant":{"__type":"Pointer",
"className":"Restaurants",
"objectId":"kIIYe7Z0tD"},...
You can't directly send the pointer objects back from cloud code even though you have included it. You need to manually copy the content of that pointer object to a javascript object. Like below:
var restaurant = {}
restaurant["id"] = YOUR_POINTER_OBJECT.id;
restaurant["createdAt"] = YOUR_POINTER_OBJECT.createdAt;
restaurant["custom_field"] = YOUR_POINTER_OBJECT.get("custom_field");
ps: in your code you seem do nothing else other than directly send the response back. I think parse REST api might be a better choice in that case.
It turned out that my code implementation was correct.

request.object.id not returning in afterSave() in Cloud Code

Parse.Cloud.afterSave(function(request) {
var type = request.object.get("type");
switch (type) {
case 'inspiration':
var query = new Parse.Query("Inspiration");
break;
case 'event':
var query = new Parse.Query("Event");
break;
case 'idea':
var query = new Parse.Query("Idea");
break;
case 'comment':
break;
default:
return;
}
if (query) {
query.equalTo("shares", request.object.id);
query.first({
success: function(result) {
result.increment("sharesCount");
result.save();
},
error: function(error) {
throw "Could not save share count: " + error.message;
}
});
}
});
For some reason request.object.id is not returning the object id from the newly created record. I've tested this code out throughly and have isolated it down to the request.object.id variable. I've even successfully ran it with using a pre-existing object ID and it worked fine. Am I using the wrong variable for the object ID?
Thanks in advanced for any help!
Had this exact problem a few weeks ago.
It turned out to be a bug in Parse's newest Javascript SDK. Please have a look at your CloudCode folder - it should contain a global.json file where you can specify the JavaScript SDK version. By default, it states "latest", change it to "1.4.2" and upload your CloudCode folder again.
In case the global.json file is missing in your cloud code folder, please have a look at this thread, where I described how to create it manually.
Thanks for the reply. I found out another work around for this for version 1.6.5. I should probably also mention that my use case for this code is to increment a count column (comments count) when a new relation has been added to a particular record (post).
Instead of implementing an afterSave method on my relation class (comment), I instead implemented a beforeSave method on my class (Post) and used request.object.dirtyKeys() to get my modified columns. From there I check to see if my dirty key was comments and if it is I increment my count column. It works pretty well actually.

What kind of object does a Parse.Query.get()

Using the JavaScript SDK, what kind of object would be returned on the following query:
var groupQuery = new Parse.Query('some_valid_class');
var group = groupQuery.get('some_valid_object_id');
console.log(group);
I only see [object Object] in the log.
thanks!
On Cloud Code - You can't print objects directly as we usually does in console of browser.
You have to use console.log(JSON.stringify(yourObject))
Although the documentation doesn't say so, I believe the get method returns a Promise. Here is an example for getting the actual object taken from the documentation:
var query = new Parse.Query(MyClass);
query.get(myId, {
success: function(object) {
// object is an instance of Parse.Object.
},
error: function(object, error) {
// error is an instance of Parse.Error.
}
});
In the success-function a Parse.Object is provided.
You can find all the info at the Parse JavaScript SDK & Cloud Code Reference
Specific Parse.Query.get() documentation here: https://parse.com/docs/js/symbols/Parse.Query.html#get

Dynamic Websites on Parse

I am trying to figure out how Dynamic Websites on Parse work.
I have followed the instructions here: https://parse.com/docs/hosting_guide#webapp to set up a basic example.
Beside cloud/views/hello.ejs, I have made cloud/views/mything.ejs and used that from app.js and it all works.
Now I would like to show for example the number of records in MyClass on Parse.
In other words, inside my Dynamic Website I want to display information related to the contents of the DB on Parse.
How can I do that? Obviously I need to include some DB query at some point, but is there any sample?
Here's an example snippet
This handles a GET request to /mypage, on execution it creates a Parse Query but returns a result count instead of a record set matching the query. On completion of the query we then set a count or error on the response using res.set(). You can then use ejs to display the count or error.
app.get('/mypage', function(req, res) {
var query = new Parse.Query('My Class');
query.equalTo("name", "Joe Blogs");
query.count({
success: function(count) {
res.set('count', count);
},
error: function(error) {
res.set('error', error);
}
});
});

Breeze filtering not working

I am using breeze and the filter does not work.
var EntityQuery = breeze.EntityQuery;
var manager = configureBreezeManager("xxx");
function configureBreezeManager(param) {
breeze.NamingConvention.camelCase.setAsDefault();
var mgr = new breeze.EntityManager(param);
model.configureMetadataStore(mgr.metadataStore);
return mgr;
}
And my query query
var query = EntityQuery.from('GetStudents').where("Id", "==", "xxx");
return manager.executeQuery(query)
The filter is ignore and all results are returned. my get student returns an IQueryable of all students.
public IQueryable<Students> GetStudents(){
return context.Students;
}
Is there something up there I am doing wrong or should I look elsewhere?
EDIT
I realize that my controller is missing the property [BreezeController]. But when I include that, me metadata path is not found giving me an error (error 500 below) when trying to load it. The matadata loads fine without this property on the controller, but filtering does not work. Is this related?
"Could not load file or assembly 'System.Web.Http.OData, ... or one of its dependencies. The system cannot find the file specified."
Your issue might be that you have specified a 'camelCase' namingConvention, but your query is for 'Id' instead of 'id'. i.e. try:
var query = EntityQuery.from('GetStudents').where("id", "==", "xxx");
return manager.executeQuery(query)
I was missing the [BreezeController] and after adding it, I received the error Could not load file or assembly System.Web.Http.OData, Version=4.0.0.0 and was able to solve it by runing Install-Package Microsoft.AspNet.WebApi.OData in the package manager

Resources