I am using masterkey yet still getting unauthorized error when trying to save an object. Can anyone figure out why? I can see that "started making ticket" gets logged. and the request.params all pull the correct information.
Also, a side question...I am under the impression that if response.error is executed it stops the code, but then will send that error message to the function that called this cloud code and then run any error handling there. I have a console.log error message in this cloud code, and then an error alert in the function that called it. I am getting the console log to show, but not the alert. Is my assumption wrong in that it doesnt get passed, and that it actually just terminates the entire thing upon executing response.error?
Parse.Cloud.define("createRecord", function(request, response) {
var caseClass = Parse.Object.extend("Cases");
var ticket = new caseClass();
console.log("started making ticket");
ticket.title = request.params.title;
ticket.category = request.params.category;
ticket.priority = request.params.priority;
ticket.description = request.params.cmnts;
ticket.save(null, {useMasterKey: true}).then(function() {
response.success();
}, function(error) {
response.error("error response: " + error.message);
});
Try this:
Parse.Cloud.run("createRecord", {something: yourData}, {...
and:
Parse.Cloud.define("createRecord", function (request, response) {
var something = request.params.something;
var caseClass = Parse.Object.extend("Cases");
var ticket = new caseClass();
console.log("started making ticket");
ticket.set("title", something.title);
ticket.set("category", something.category);
ticket.set("priority", something.priority);
ticket.set("description", something.cmnts);
ticket.save(null, {useMasterKey: true}).then(function() {
response.success();
}, function(error) {
response.error("error response: " + error.message);
});
Related
since a couple of hours ago all my parse functions have been returning invalid json. Nothing to do with the cloud code... I even tried rolling it back. I'm on the android platform and haven't made any game breaking changes to it..
For example,
I have a login function...
Parse.Cloud.define("loginuser", function(request, response){
var useremail = request.params.useremail;
var userpassword = request.params.userpassword;
var usersource = request.params.usersource;
Parse.User.logIn(useremail, userpassword,{
success:function(user){
// Sets either candidate or business to be true depending on condition
if (usersource == "candidate"){
user.set("candidate", true);
} else if (usersource == "business"){
user.set("business", true);
}
user.save(null, {
// login success & return
success: function(user){
response.success(user);
}, error: function(error){
response.error(error);
}
});
},
error:function(user, error){
// login failure
response.error(user, error);
}
});
});
With no change to it... it suddenly starts throwing error:
01-05 22:37:30.175 1052-1052/recruitr.recruitr E/Login error: com.parse.ParseRequest$ParseRequestException: bad json response
01-05 22:37:46.045 1052-1052/recruitr.recruitr E/Signup Error: com.parse.ParseRequest$ParseRequestException: bad json response
Does anyone know why?
EDIT:
Ran debugger and it pops this out when the error message shows up:
this = {LoginActivity$4#4619}
cancel = {boolean[1]#4623}
logincredentials = {HashMap#4624} size = 3
parseUser = null
e = {ParseRequest$ParseRequestException#4625} "com.parse.ParseRequest$ParseRequestException: bad json response"
isPermanentFailure = false
code = 100
cause = {JSONException#4630} "org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject"
cause = {JSONException#4630} "org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject"
detailMessage = {String#4638} "Value <html> of type java.lang.String cannot be converted to JSONObject"
stackState = {long[34]#4639}
stackTrace = {StackTraceElement[0]#4633}
suppressedExceptions = {Collections$EmptyList#4634} size = 0
shadow$_klass_ = {Class#497} "class org.json.JSONException"
shadow$_monitor_ = -1960135782
detailMessage = {String#4631} "bad json response"
stackState = {long[30]#4632}
stackTrace = {StackTraceElement[0]#4633}
suppressedExceptions = {Collections$EmptyList#4634} size = 0
shadow$_klass_ = {Class#4592} "class com.parse.ParseRequest$ParseRequestException"
shadow$_monitor_ = -2123277170
Found the problem:
The Android Parse SDK was having problems with a new update. Parse was not actually initializing in the code at all (keys weren't working/initialization was not working)
Fix the error by changing the dependency:
compile 'com.parse:parse-android:1.+'
to
compile 'com.parse:parse-android:1.12.0'
or
compile 'com.parse:parse-android:1.10.0'
Both seem to work flawlessly as of this moment in time.
With the Javascript code below, do I need to define the name of my Parse database (_User) I want it to access? At the moment I'm getting 'error code: 141', so I thought maybe it doesn't know what database I want it to query, since I have more than one.
Parse.Cloud.define("test", function(request, response) {
Parse.Cloud.useMasterKey();
var friendRequestId = request.params.friendRequest;
var query = new Parse.Query("FriendRequest");
query.get(friendRequestId, {
success: function() {
response.success("Success!");
},
error: function(error) {
response.error(error);
}
});
});
For those who want to see where I call the PFCloud
[PFCloud callFunctionInBackground:#"test" withParameters:#{#"friendRequest" : friendRequest.objectId} block:^(id object, NSError *error) {
if (!error) {
NSLog(#"Success");
}
else {
NSLog(#"Error");
}
}];
Thanks!
What do you mean, which database you want it to access? You define the class you'd like to query new Parse.Query("class"); So you're querying through the FriendRequest class. Perhaps your class name isn't actually FriendRequest? It's case sensitive.
I recommend that you use Parse Promises rather than callbacks. Also, an alternative to querying for an object based on id is fetching the object based on id. Here's my suggestion for what your cloud code should look like:
**Note, OP requested some additional help in the comments, so this answer is being updated to reflect his other questions.
Parse.Cloud.define("test", function( request, response )
{
Parse.Cloud.useMasterKey() //You only need this if you're, say, editing a user that didn't make this request or accessing an object that users don't have access to
var FriendRequest = Parse.Object.extend("FriendRequeset");
var friendRequestId = request.params.friendRequest;
var friendRequest = new FriendRequest();
var user1;
var user2;
friendRequest.id = friendRequestId;
friendRequest.fetch().then
(
function( friendRequest )
{
user1 = friendRequest.get("user1");
user2 = friendRequest.get("user2");
return Parse.Object.fetchAll([user1, user2]);
},
function( error )
{
response.error("Error fetching FriendRequest: " + error.message); //This will give you a bit more of a description of your errors
}
).then //fetchAll
function( objects )
{
//set up your relations. I've never used relations, but I'm sure you can find some examples
return Parse.Object.saveAll([user1, user2]);
},
function( error )
{
response.error("Error fetching users: " + error.message);
}
).then //saveAll
(
function( objects )
{
response.success("Success!");
},
function( error )
{
response.error("Error saving the users: " + error.message);
}
);
});
Notice I added some output to your error call. That'll help you a lot while debugging down the road. Get used to doing that.
And finally, check your logs from your Parse dashboard. It usually tells you what line you hit an error on. Look for the "on main.js: xxxx:yy" the xxxx is your line number where the error occurred.
edit - I saw the NSLogs from your obj-c code and mistook those for your response.success/error calls from cloud code. If you saw this before I edited it, I totally lied about that probably being your error :p
Context
I am using Parse.com after my backend for a web application. The problem I am having relates to Cloud Code.
After a user completes their profile, I want to call an afterSave( ) function which updates a few fields in the dataset MY_TABLE.
The fields being updated (variable01, variable02, and variable03) are retrieved via the function MY_FUNCTION with inputs param01 and param02.
Error
I get the following error, which I cannot fix: "Uncaught Tried to save an object with a pointer to a new, unsaved object."
Code
Parse.Cloud.afterSave("PROFILE_COMPLETE", function(request) {
// Use Master Key
Parse.Cloud.useMasterKey();
// Identify Parameters in Request
var param01 = request.user.get('param01');
var param02 = request.user.get('param02');
// Print Parameters to Console
console.log("Parameter 01 (" + param01 + ") successfully identified");
console.log("Parameter 02 (" + param02 + ") successfully identified");
// Identify Variables by Calling MY_FUNCTION()
Parse.Cloud.run('MY_FUNCTION', {param01: param01, param02: param02}, {
success: function(results) {
// Print Results
console.log("Function MY_FUNCTION() returned successful response");
console.log(results);
// Print Result Variables
console.log("Identified variable01 = " + results.variable01);
console.log("Identified variable02 = " + results.variable02);
console.log("Identified variable03 = " + results.variable03);
// Do I need to extend MY_TABLE or something else here???
// I am doing the update and save wrong...
// Update 'Complete' Data
request.object.set("variable01", results.variable01);
request.object.set("variable02", results.variable02);
request.object.set("variable03", results.variable03);
// Save Variables to 'MY_TABLE'
request.object.save(null, {
success: function(results) {
console.log("Variables successfully updated in MY_TABLE dataset");
console.log(results);
},
error: function(error) {
console.log("Error when saving variables in MY_TABLE dataset");
console.log("Error " + error.code + ": " + error.message);
}
})
},
error: function(error) {
console.log("There was an error when running function MY_FUNCTION()");
console.log(error);
}
});
});
Probably missing some information in your description but I hope my previous experience can help:
Last time I got this error, I was doing the following:
context: inside a query callback where the result variable is res
var MyParseClass = Parse.Object.extend("MyParseClass");
var newObject = new MyParseClass();
res.set("pointerFieldToMyParseClass",newObject);
res.save();
In this example, newObject doesn't have an existence outside the scope of this function, so you must save it first:
var MyParseClass = Parse.Object.extend("MyParseClass");
var newObject = new MyParseClass();
newObject.save({
success:function(savedObject){
res.set("pointerFieldToMyParseClass",newObject);
res.save();
},error:function(e){}});
Im trying to send a push message to everyone with read access every time a new note is saved.
In pseudocode it should get the ACL. Evaluate each member in the ACL and return an array of all users with read access. Then send a push notification to each member.
I've tried running separate task one by one and it works properly. However when I put everything together in the following code I get strange results. Looking at the log I can see it not executing in order as I expect. I first though the getACL call was an asynchronous call so I tried to implement promises with no luck. Then after help from stackoverflow I find out that getACL is not asynchronous therefore the code should just work, right?
This is what I'm trying:
Parse.Cloud.afterSave("Notes", function(request) {
var idsToSend = [];
var i = 0;
console.log("1 start");
var objACL = request.object.getACL();
var ACLinJSON = objACL.toJSON();
console.log("2 ACL = " + ACLinJSON);
for (var key in ACLinJSON) {
if (ACLinJSON[key].read == "true") {
idsToSend[i] = key.id;
console.log("3 i = " + i + " = " + idsToSend[i]);
i++;
}
}
console.log("4 idsToSend = " + idsToSend);
//lookup installations
var query = new Parse.Query(Parse.Installation);
query.containedIn('user', idsToSend);
Parse.Push.send({
where: query,
data: {
alert: "note updated"
}
}, {
success: function() {
console.log("Success sent push");
},
error: function(error) {
console.error("can’t find user"); //error
}
});
});
And this is the response I see from parse log
I2014-08-04T08:08:06.708Z]4 idsToSend =
I2014-08-04T08:08:06.712Z]2 ACL = [object Object]
I2014-08-04T08:08:06.714Z]1 start
I2014-08-04T08:08:06.764Z]Success sent push
Everything is completely out of order??
How can I execute the above function in the way it's written?
I've found the logs are not in order when I run things too, could be a timing issue or something, ignore the order when they're in the same second, I have done other tests to confirm things really do run in order on my own Cloud Code... had me completely confused for a while there.
The issue you're having is that log #3 is never being hit... try tracing ACLinJSON on it's own to see the actual structure. When you append it to a string it outputs [object Object] as you have seen, so do console.log(ACLinJSON); instead.
Here's the structure I've seen:
{
"*":{"read":true},
"Administrator":{"write":true}
}
Based on that I would expect your loop to work, but it may have a different level of wrapping.
UPDATE:
Turns out the issue was looking for the string "true" instead of a boolean true, thus the fix is to replace the following line:
// replace this: if (ACLinJSON[key].read == "true") {
if (ACLinJSON[key].read == true) {
I have an object Enquiry which has a one to many relation with History objects.
My requirement is that when user asks for history I need to fetchAll the history objects and then display them
Here is my code:
function fetchAndShowHistory(object, objectType) {
var historyObjects = object.get("history");
Parse.Object.fetchAll(historyObjects, {
success : function(historyObjects) {
//code to show history objects
},
error : function(error) {
alert("Could not fetch history, " + error.message);
},
});
}
Now with this current code I get this error:
Uncaught TypeError: Object function (a,d){if(c.isString(a))return
b.Object.create.apply(this,arguments);a=a||{},d&&d.parse&&(a=this.parse(a));var
e=b.getValue(this,"defaults");if(e&&(a=c.extend({},e,a)),d&&d.collection&&(this.collection=d.collect......l'
Note that this doesn't even make the call to the server but dies in the javascript code. Any idea what is wrong here?
There is a ',' on the 2nd-last line of your code. This is a syntax error and will cause your code to fail.
This is the correct code ->
function fetchAndShowHistory(object, objectType) {
var historyObjects = object.get("history");
Parse.Object.fetchAll(historyObjects, {
success : function(historyObjects) {
//code to show history objects
},
error : function(error) {
alert("Could not fetch history, " + error.message);
}
});