Uncaught SyntaxError: Unexpected string in my javascript file - javascript

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);
}
});
});

Related

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);
}
});
}

Parse updating a set of objects

I have trouble updating a set of values in my cloud code. I have tried .save() seperately and .saveAll() but the class doesn't get updated in Parse and I get errors returned.
What I am trying to do is to get all messages from class ChatMessages which has a pointer to the Parse user and Chat class. When the method is called, the class column readAt needs to be updated to the current date. I call my method from an iOS (objective-C) app.
This is the latest version of my method:
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var errorMsg;
var roomName;
var _ = require('underscore.js');
var userPointer = new Parse.User.current();
if (!request.params.roomName) {
errorMsg = "Chat room needs to be identified";
} else {
roomName = request.params.roomName;
}
console.log("Checking chats for userID: " + userPointer.id);
if (!userPointer.id) {
var emptyUserMsg = "User has to be provided";
if (errorMsg) {
errorMsg = errorMsg + emptyUserMsg;
} else {
errorMsg = emptyUserMsg;
};
}
if (errorMsg) {
response.error(errorMsg);
}
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
chatQuery.find({
success: function(results) {
if (results.length > 0) {
var chat = results[0];
console.log("Found chat with ID: "+chat.id);
var chatMessagesQuery = new Parse.Query("ChatMessage");
chatMessagesQuery.equalTo("chat", chat);
chatMessagesQuery.notEqualTo("fromUser", userPointer);
chatMessagesQuery.equalTo("readAt", undefined);
chatMessagesQuery.find().then(function(chatMessagesQueryResults) {
_.each(chatMessagesQueryResults, function(result) {
result.set("readAt", new Date());
console.log("Setting readAt for chat message " + result.id + " which has toUser " + result.get("toUser"));
});
return Parse.Object.saveAll(chatMessagesQueryResults,{
success: function(list) {
console.log("Success updating objects");
},
error: function(error) {
console.log("Error updating objects: " + error);
},});
}).then(function(results) {
response.success(results);
console.log("Update for reatAt for chat is successfull");
}, function(error) {
response.error(error);
console.log(error);
});
} else {
response.error("No rooms found");
console.log("No rooms found");
}
},
error: function(error) {
response.error("Room name not found");
console.log(error);
}
});
});
Log output:
E2015-07-19T09:13:48.483Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
I2015-07-19T09:13:48.540Z]Checking chats for userID: CZwQL4y751
I2015-07-19T09:13:48.593Z]Found chat with ID: gfvAkirqTs
I2015-07-19T09:13:48.647Z]Setting readAt for chat message ZiWUIdUtUm which has toUser undefined
I2015-07-19T09:13:48.648Z]Setting readAt for chat message YHEBLpR04U which has toUser undefined
I2015-07-19T09:13:48.649Z]Setting readAt for chat message 0wZ4LQd8ZC which has toUser undefined
I2015-07-19T09:13:48.650Z]Setting readAt for chat message MYsYGyXI0k which has toUser undefined
I2015-07-19T09:13:48.751Z]Error updating objects: [object Object]
I2015-07-19T09:13:48.752Z]{"code":101,"message":"object not found for update"}
E2015-07-19T09:13:49.042Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
Class:
The query can be vastly simplified by making a chatMessages query relational to chats matching the user and room criteria. The code structure can be improved by not mixing callback and promise styles, and by separating logical chunks into small, promise-returning functions.
Stripping away some of the debug instrumentation you added, we get (untested, of course)...
function unreadChatMessagesInRoom(roomName, excludeUser) {
var query = new Parse.Query("ChatMessage");
query.notEqualTo("fromUser", excludeUser);
query.doesNotExist("readAt");
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
query.matchesQuery("chat", chatQuery);
return query.find();
}
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var _ = require('underscore.js');
var user = request.user;
unreadChatMessagesInRoom(request.params.roomName, user).then(function(chatMessages) {
console.log(chatMessages.length + " chat messages found");
_.each(chatMessages, function(chatMessage) {
chatMessage.set("readAt", new Date());
});
return Parse.Object.saveAll(chatMessages);
}).then(function(results) {
response.success(results);
}, function(error) {
response.error(error);
});
});

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);

Find objects with field value (this is a pointer)

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);
}
});
};

Uncaught SyntaxError: Unexpected token using an else statement in JavaScript code block

Why do I get a "Uncaught SyntaxError: Unexpected token" else eror in Chrome tools using this code, I thought I should just use else here not else if ?
var friendName;
function findFriend(){
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function(friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
}
else {
// Query executed with success
{ alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
$('#find_button').click(function(e){
findFriend();
});
With reasonable (auto-)indentation, you spot the error(s) easily:
function findFriend() {
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function (friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
} else {
// Query executed with success
{
alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
The braces around the else are too much, it should be
function findFriend() {
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function (friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
else // Query executed with success
alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
or
…
success: function (friendMatches) {
if (friendMatches.length === 0) {
alert('NO MATCH FOUND!!!');
} else {
// Query executed with success
alert('MATCH FOUND!!!');
}
},
…
change
{ alert('MATCH FOUND!!!');
to
alert('MATCH FOUND!!!'); }

Categories

Resources