So, my Javascript isn't the best but I have to venture into it to run some Cloud Code functions. I have the following:
Parse.Cloud.define("setCommentIsTitle", function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query("Comment");
query.equalTo("objectId", request.params.objectId);
query.first({
success: function(object) {
object.set('isTitle', request.params.isTitle);
return object.save(); },
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
I logged the objectId I'm passing in as request.params.objectId and it's correct. What I don't get is that success is being called, but then I'm getting the following Cloud Code log when I console.log object:
I2013-10-21T17:27:52.120Z] object = undefined
And the following error returned in XCode:
code=141, error=TypeError: Cannot call method 'set' of undefined
If I'm calling the first function on query, and success is being called, shouldn't that mean there is an object returned? Why is object undefined?
OK, so this was a stupid error on my part, but also abetted by a confusing Parse error message.
My class is called Comments and not Comment, so I was looking up the wrong class. However, since success was called on the query I started looking in all the wrong places for the error.
Why would success be called if I'm querying a class that doesn't even exist??
Related
I'm trying to send an error event object from the content script:
catch (error) {
chrome.runtime.sendMessage(sender.id, error);
}
to the background script:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
console.log(msg)
});
in order to log it there with the full stack trace and all the relevant data,
but it always arrives as undefined.
I tried to JSON.Stringify the error and send it in an object, but it arrives as an empty object.
From https://developer.chrome.com/docs/extensions/reference/runtime/:
If I'm sending error.message and error.stack separately, creating a new Error object works fine:
let error = new Error();
error.message = msg.message
error.stack = msg.stack;
How can I send the error object and receive it as-is?
You can't send the error "as-is", since the error object will not survive the async call - it will have gone out of scope. The reason JSON.stringify doesn't work is that the properties of the error are not enumerable because they belong to the error object's prototype chain - they aren't "owned properties". See this answer for a discussion and solution. Basically, you need to copy the properties of the error object to a new object and pass that.
I am trying to setup Cloud Code in Parse for Mailgun to send emails. I have successfully done with writing the Java Script code mentioned below
Parse.Cloud.define("SendEmail", function(request, response) {
var Mailgun = require('mailgun');
Mailgun.initialize('myDomainName', 'MyKey');
Mailgun.sendEmail({
to: request.object.get("to") ,
from: "info#sample.com",
subject: request.object.get("subject"),
text: request.object.get("text")
},{
success: function() {
response.success(request.params);
console.log("--email sent - success");
console.log(request.params);
},
error: function() {
console.log("--failed to send email - success");
console.error(request.params);
response.error("Uh oh, something went wrong");
}
});
});
But I am continuously getting the following error
Error: TypeError: Cannot call method 'get' of undefined
at main.js:1:602 (Code: 141, Version: 1.2.20)
2014-08-20 02:05:03.725 PhotoAlert[475:60b] Error : Error Domain=Parse Code=141 "The operation couldn’t be completed. (Parse error 141.)" UserInfo=0x15ec5b90 {code=141, error=TypeError: Cannot call method 'get' of undefined
at main.js:1:602}
I am anxiously looking for solution as I am new for Parse/Mailgun and JS too.
Regards
I believe that where you have:
request.object.get("to")
It should be:
request.params.to
And that goes for all of the parameters that you are passing into the CloudCode Function (to, subject and text).
Being very new to this myself, Im guessing that the error is saying that the is no argument being passed in called "object" therefor there is no "get" method for it.
I am trying to build a simple background job on the Parse Cloud. Right now, I'm just testing, but I am having a problem when performing a query.
If I comment out:
//query.ascending("createdAt");
the console log shows all the messages and no errors. If I don't comment it out, I get an error. Can anybody explain why this is happening? Is it an authentication error?
Parse.Cloud.job("cleanPosts", function(request, status) {
var Post = Parse.Object.extend("Post");
var query = new Parse.Query(Post);
query.ascending("createdAt");
query.each(function(post) {
console.log( "objectId:" + post.get("message") );
}).then(function() {
status.success("Success");
}, function(error) {
status.error();
});
});
When using Parse.Query.each, you do not need to (and cannot) provide an orderBy. It will run the callback for every object (actually ordered by objectId).
The official error is "Cannot iterate on a query with sort, skip, or limit." and it should appear if you log that in the error block.
The following code doesn't save anything to the database:
var UserObject = Parse.Object.extend("User");
var objectid = object.id;
var secondQuery = new Parse.Query(UserObject);
secondQuery.get(objectid, {
success: function(userObject) {
alert(userObject.get("fbId"));
userObject.set("provider_access_token", access_token);
userObject.set("provider_refresh_token", refresh_token);
userObject.set("provider_token_expire", expires_in);
userObject.save(null, {
error: function(error){
alert(error.message + error.code);
}
});
},
error: function(object, error) {
alert("Error:" + error.code + " " + error.message);
}
});
I'm not sure why, but it does give me an 400 HTTP error. What am I doing wrong?
I've checked that all my variables are set and correct (the alert works just fine).
You have a typo: errror > error
Fixing this will give you the real error of why isn't getting saved
Thanks to Juan Guarcia who pointed out that I had an type which led to the error not showing up. After fixing that my problem was easily solved.
The error I was getting was the following:
Parse::UserCannotBeAlteredWithoutSessionError206
Which means that user objects cannot be altered without using the master key of my application. However, this isn't supported inside the Parse Javascript SDK, only in the Cloud Code.
So I need to move the function to the Cloud Code and then save from there. Problem solved!
I'm using javascript and parse.com
The below section of code should query the parse.com back end and look for users that exist called "Rob". When inspecting it using Chrome dev tools no errors are returned to the console.
However the code always completes successfully, even using the example shown where I know that there is not a user called "Rob" stored in that parse object.
I dont understand what I'm missing in my code or why it wont error if the user does not exist?
var friendFinder = Parse.Object.extend("_User");
var query = new Parse.Query(Parse.User);
query.equalTo("username", "Rob"); // find users that match
query.find({
success: function(results) {
console.log("Yay");
},
error: function (contact, error) {
//Show if no user was found to match
alert("Error: " + error.code + " " + error.message);
}
})
;
Not finding a row is not an error condition. The result of the call was successful, and your results were empty.