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.
Related
I will preface this post with I am very new to JavaScript and even newer to AWS and their services.
I am currently working through this AWS tutorial and I am adapting it to fit my needs. I am not asking to make this work, as I already have it working for my needs.
In this bit of code
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
});
I am having trouble reformatting the err received by the function call when something goes wrong.
For example, I am using AWS Lambda to check the input validation for server side (using python), and I have it raise Exception("First Name is not long enough") which is what i get in return. However, when the alert(err) is called, correctly, I receive this:
UserLambdaValidationException: PreSignUp failed with error First Name is not long enough..
I have tried split on the err but the console says split is not a function of err.
So my question is, how can I strip this err and only get the message instead of the whole exception?
I have already tried err.split("error"); err.value; err.errorMessage; err["value"]; err["errorMessage"]; err.Error; and it doesn't work.
Also, when I console.log(err) I am presented with:
Error: First Name is not long enough..(...) //the (...) is the stacktrace
Parse.initialize(ApiKeys.appId, ApiKeys.jsKey, ApiKeys.masterKey);
function submit(){
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.save({
score: 1337,
playerName: "Sean Plott",
cheatMode: false
}, {
success: function(gameScore) {
// The object was saved successfully.
debug_log("score success");
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
debug_log("score error");
}
});
}
I am sure that I have already config the keys. But I still cannot save, everytime, when I submit, it give me back error.
I use back{4}app instead of parse, is there something important was ignored by me?
Thanks in advance.
You should provide the error code and its message.
The code seems ok, check your keys and settings.
Check the error message
not authorized=> keys not matched.
something like you are without permission to XXX=> check the class level permission (CLP)
or you have the beforeSave on cloud code, check response.success() have been called.
Add
Parse.serverURL="back4app api URL"
Solve the ptoblem.
Here is the Parse javascript cloud code I am trying to use. As a new _User is created I want to add them to my 'Client' Role.
Parse.Cloud.afterSave(Parse.User, function(request) {
Parse.Cloud.useMasterKey();
query = new Parse.Query(Parse.Role);
query.equalTo("name", "Client");
query.first ({
success: function(role) {
role.getUsers().add(request.user);
role.save();
},
error: function(error) {
throw "Got an error " + error.code + " : " + error.message;
}
});
});
This is taking code directly from Parse.com's Role example. The code runs happily when a new _User is saved, returning Result: Success, but when I check the "users" tied to that Role in the Data Browser, nothing has happened.
I have also tried substituting role.getUsers().add(request.user); for role.relation("users").add(request.user); as per an example on Parse.com's old forum, but no difference. This seems like it should be really straight forward, so I'm not sure what I'm doing wrong.
(I have manually used the REST API, using curl, to add _Users to the Client Role, and this does work, so I know it should work.)
Turns out you need to use request.object instead of request.user. Now it works!
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.