Find objects with field value (this is a pointer) - javascript

I have class say 'inspection' and there is a field name property (this is a pointer) . I am trying to retrieve a record by field value but getting nothing as a result. I am using following code
getInspectionByProperty = function(req) {
console.log(req.body.propertyId)
var query = new Parse.Query("Inspection");
query.include('property');
query.equalTo('property', req.body.propertyId);
query.find({
success: function(data) {
console.log('in success');
console.log(data);
// Successfully retrieved the object.
},
error: function(error) {
console.log('in error')
console.log("Error: " + error.code + " " + error.message);
}
});
};
I am getting this error in parse log
Error: 102 pointer field property needs a pointer value
How can i get the record .Thanks in advance

Here what i try and thats work for me.
getInspectionByProperty = function(req) {
console.log(req.body.propertyId)
var query = new Parse.Query("Inspection");
query.include('property');
query.equalTo("property", {
"__type": "Pointer",
"className": "Property",
"objectId": propertyId
});
query.find({
success: function(data) {
console.log('in success');
console.log(data);
// Successfully retrieved the object.
},
error: function(error) {
console.log('in error')
console.log("Error: " + error.code + " " + error.message);
}
});
};

Related

Uncaught SyntaxError: Unexpected string in my javascript file

This is my likedc.js which I tried to upload but however the terminal reported me that it is failed because Error: Uncaught SyntaxError: Unexpected string in likedc.js:40 at main.js:3:1
I tried to comment but the terminal still says the same thing. I'm not sure what are the alternatives that I can amend
Parse.Cloud.define("StatusUpdate", function(request,response){
//query Installation for user
var Installationquery = Parse.Object.extend("Installation");
var query = new Parse.Query(Installationquery);
var message = request.params.message
query.equalTo("user", request.params.User);
query.find({
success: function(results) {
response.success("found user" + results)
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: createMessage(message)
badge: "Increment", **//<---- Line 40**
sound: "";
}
}, {
success: function() {
// Push was successful
console.log("sent ")
},
error: function(error) {
console.log("Error " + error)
}
}
},
error: function(error) { // <--- Line 54**
alert("Error: " + error.code + " " + error.message);
}
});
})
});
var alertMessage = function createMessage(request)
{
var message = "";
if (request.object.get("StatusUpdate") === "likedby") {
if (request.user.get('postedby')) {
message = request.user.get('postedby') + ': ' + request.object.get('statusOBJID').trim();
} else {
message = "Someone liked on your status update.";
}
// Trim our message to 140 characters.
if (message.length > 140) {
message = message.substring(0, 140);
}
return message;
}
}
data: {
alert: createMessage(message), //<-- add comma here
badge: "Increment", **//<---- Line 40**
sound: "" //<-- remove semicolon here
}
For the second part, from the context I think your error block is out of place, try rearranging like
Parse.Cloud.define("StatusUpdate", function(request, response) {
//query Installation for user
var Installationquery = Parse.Object.extend("Installation");
var query = new Parse.Query(Installationquery);
var message = request.params.message
query.equalTo("user", request.params.User);
query.find({
success: function(results) {
response.success("found user" + results)
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: createMessage(message),
badge: "Increment", //<---- Line 40**
sound: ""
}
}, {
success: function() {
// Push was successful
console.log("sent ")
},
error: function(error) {
console.log("Error " + error)
}
});
}
},
error: function(error) { // <--- Line 54**
alert("Error: " + error.code + " " + error.message);
}
});
});

Parse Cloud Code function get deviceToken By username

I am kinda new to the whole cloud code and I am having a bit of trouble fetching some data.
So what I basically want is a function that by giving it a 'username' that is located in the Parse.User database, it will return the user's objectId which I will then use to locate the user's session in the Parse.Session, from there I will get the installationId which I will then use in the Parse.Installation to get the device token.
Note: I have written a function that keeps only 1 session active per user.
My issue:
The Query of Parse.Session has a result which only contains 3 objects, and the installationId is not included,therefor I can not find out what is the installation id and then use it to search the Parse.Installation to get the device token.
input example:
Input:
{"from":"user1",
"msg":"hello",
"title":"Whatever title",
"to":"user2"}
Here is the existing code I currently have which doesn't work.
Parse.Cloud.define('gcm', function(request,response){
var username = request.params.to;
console.log("usr "+username);
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('username', username);
userQuery.find({
success: function(results){
var objectId = results[0].id;
console.log("obj id "+objectId);
var user = new Parse.User();
//Set your id to desired user object id
user.id = objectId;
var sessionQuery = new Parse.Query(Parse.Object.extend('Session'));
sessionQuery.include(user);
sessionQuery.find({
success: function(results1){
console.log("result classname type: "+typeof(results1[0]));
var installId = results1[0].installationId ; //here is the value which I want from the result, but the object is type of _Session which does not have installationId.
console.log("inst id "+installId);
var InstallationQuery = new Parse.Query(Parse.Installation);
InstallationQuery.equalTo('installationId',installId);
InstallationQuery.find({
success: function(results2){
var deviceToken = results2[0].get("deviceToken");
console.log("token "+deviceToken);
Parse.Cloud.httpRequest({
method: "POST",
url: " https://gcm-http.googleapis.com/gcm/send",
headers: {'Authorization' : 'key=AIzaSyDj4ISkLW7CzAQEQEhTsq3JYZK5OP8tSzY',
'Content-Type' : 'application/json'},
body: {
"data":
{
"title": request.params.title,
"msg": request.params.msg
},
"to" : deviceToken
},
success: function(httpResponse) {
response.success("Message Sent!");
console.log(httpResponse.text);
},
error: function(httpResponse) {
response.error("Error, Something went wrong.");
console.log("error 4: " + httpResponse.status);
}
});
},
error: function(error) {
//error
console.log("error 3: " + error);
}
});
},
error: function(error) {
//error
console.log("error 2: " + error);
}
});
},
error: function(error) {
//error
console.log("error 1: " + error);
}
});
});

Trying to add player object to an array of players within my session

I'm trying to create a Lobby for a web game I'm working on and I want to add the person who is creating the lobby to the lobby's "Players" array. I'm using parse and I can't for the life of me figure out how to append just the player object into the array. Any help is great!
function createLobby(){
var ses = Parse.Object.extend("Session");
var query = new Parse.Query(ses);
var exists = false;
query.equalTo("sessionName", document.getElementById("lobby").value);
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length );
if(results.length!=0){
alert("Session already exists");
exists = true;
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
if(exists==false){
session.set("sessionName",document.getElementById("lobby").value);
var Players = Parse.Object.extend("Player");
var query = new Parse.Query(Players);
query.equalTo("Name", "testName");
query.find({
success: function(results) {
var query2 = new Parse.Query(Players);
query2.get(results[0].id, {
success: function(k) {
console.log(k);
session.add("Players", k);
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
session.save(null, {
success: function(object) {
$(".success").show();
},
error: function(model, error) {
$(".error").show();
}
});
}
}
You're failing to understand how callbacks work. I recommend you read about JavaScript callbacks and promises. Here's how this should be written:
function createLobby() {
var ses = Parse.Object.extend("Session");
var query = new Parse.Query(ses);
var exists = false;
query.equalTo("sessionName", document.getElementById("lobby").value);
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length);
if (results.length != 0) {
alert("Session already exists");
return;
}
session.set("sessionName", document.getElementById("lobby").value);
var Players = Parse.Object.extend("Player");
var query = new Parse.Query(Players);
query.equalTo("Name", "testName");
query.find({
success: function(results) {
var query2 = new Parse.Query(Players);
query2.get(results[0].id, {
success: function(k) {
console.log(k);
session.add("Players", k);
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
session.save(null, {
success: function(object) {
$(".success").show();
},
error: function(model, error) {
$(".error").show();
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}

Promises on cloud code

I'm writing a cloud code function for run many task after delete an object.
How can I do this cleanly and without nesting?
You can run tasks in parallel or it is better to have them sequentially? If so, how?
This is my code, can you help me to clean/change?
Parse.Cloud.afterDelete("Photo", function(request) {
Parse.Cloud.useMasterKey();
//delete all related activities
var queryAct = new Parse.Query("Activity");
queryAct.equalTo("photo",{
__type: "Pointer",
className: "Photo",
objectId: request.object.id
});
queryAct.limit(1000);
queryAct.find({
success: function(activities) {
Parse.Object.destroyAll(activities, {
success: function() {
//delete all related hashtags
var queryTags = new Parse.Query("hashTags");
queryTags.equalTo("photo",{
__type: "Pointer",
className: "Photo",
objectId: request.object.id
});
queryTags.limit(1000);
queryTags.find({
success: function(hashtags) {
Parse.Object.destroyAll(hashtags, {
success: function() {},
error: function(error) {
console.error("Error deleting related hashtags " + error.code + ": " + error.message);
}
});
},
error: function(error){
console.error("Error finding related hashtags " + error.code + ": " + error.message);
}
});
},
error: function(error) {
console.error("Error deleting related activities " + error.code + ": " + error.message);
}
});
},
error: function(error) {
console.error("Error finding related activities " + error.code + ": " + error.message);
}
});
});
Thanks to all!
Here find.then.delete is a series of tasks in a row. However, you can delete your hastags and activities in parallel since they don't seem to be dependent. Please check promises guide for more info.
Your code can be shortened as below:
var Photo = Parse.Object.extend("Photo");
Parse.Cloud.afterDelete("Photo", function(request) {
Parse.Cloud.useMasterKey();
var photo = Photo.createWithoutData(request.object.id);
var queryAct = new Parse.Query("Activity");
queryAct.equalTo("photo", photo);
queryAct.limit(1000);
var promiseForActivities = queryAct.find().then(function (activities) {
return Parse.Object.destroyAll(activities);
});
var queryTags = new Parse.Query("hashTags");
queryTags.equalTo("photo", photo);
queryTags.limit(1000);
var promiseForHashtags = queryTags.find().then(function (hashtags) {
return Parse.Object.destroyAll(hashtags);
});
return Parse.Promise.when([promiseForActivities, promiseForHashtags]).then(function () {
console.log("Done");
}, function (err) {
console.error(err);
});
});
Note that you don't need to create your own pointer objects like:
{
__type: "Pointer",
className: "Photo",
objectId: "someId"
}
Rather you can use createWithoutData which is a shortcut for:
var Photo = Parse.Object.extend("Photo");
var p = new Photo();
p.id = "someId";
I also think that it could be enough to pass the request.object directly, like queryAct.equalTo("photo", request.object);

Get coupons from stripe in parse.com cloud code

I am trying to connect to stripe and retrieve a coupon, but am not able to. If I run either of these, I get an error.
Stripe.Coupons.retrieve(couponId, {
success: function(coupon) {
logger.log('debug', "coupon found", coupon);
return callback(coupon, null);
},
error: function(error) {
logger.log('debug', "coupon not found", error);
return callback(null, error);
}
});
I get back an error - invalid_request_error
or
Stripe.Coupons.retrieve({
couponId: couponId
}, {
success: function(coupon) {
logger.log('debug', "coupon found", coupon);
return callback(coupon, null);
},
error: function(error) {
logger.log('debug', "coupon not found", error);
return callback(null, error);
}
});
I get back an error - couponId required
There does not seem to be any good documentation out there about how to accomplish this.
why aren't you using parse.com guide? I am not strong with backbone.js but if you have a table called Coupons you can do this:
var Coupons = Parse.Object.extend("Coupons");
var couponsQuery = new Parse.Query(Coupons);
couponsQuery.equalTo(objectId,couponId);
couponsQuery.first.then(
function(coupon)
{
//success, coupon object returned from query callbeck
}
function(error)
{
console.log("error: " + error.message);
}
);
The code above uses promise, but you can use it in the old way like this:
couponsQuery.first.then(
success: function(coupon)
{
//success, coupon object returned from query callbeck
}
error: function(error)
{
console.log("error: " + error.message);
}
);
I hope this will help you.

Categories

Resources