Parse save method gives 400 error - javascript

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!

Related

Getting Error: First argument to verifyIdToken() must be a Firebase ID token string

I'm making an XMLHttpRequest via POST method and then trying to get the tokenid to verify it in my node.js file. However, I keep on getting an error when I try to verify the token. I get an error saying:
Error: First argument to verifyIdToken() must be a Firebase ID token string
This is how I'm making the POST request:
var xhr = new XMLHttpRequest();
xhr.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
xhr.onload = function() {
console.log("Signed in as: " + xhr.responseText);
};
xhr.send("idtoken=" + user.getIdToken());
This is my code for the node.js file:
app.post("/tokensignin", (req, res) => {
admin
.auth()
.verifyIdToken(req.body.idtoken)
.then(function(decodedToken) {
let uid = decodedToken.uid;
console.log("uid is " + uid);
})
.catch(function(error) {
console.log(error);
});
});
I have tried using req.body, and req.body.token, but the error persists. When I try to print the idtoken, I get [Object object]
You are sendinging data using the field name called "idtoken":
xhr.send("idtoken=" + user.getIdToken());
But you are accessing it on your backend using a different name "token":
.verifyIdToken(req.body.token)
I suggest doing more logging in general in order to better understand what you're working with on both sides and debug what's going on.
What do you get when logging the request.body object? You have nodejs body parser installed, right? Are you using your own NodeJS server (I am almost certain you do)? But my question is why not use the Firebase environment?

Parse.com CloudCode add user to existing role not working

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!

Parse Cloud query error

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.

Code wont return a error trying to find a user , even when the user does not exist in the database

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.

Parse.com -- Cloud Code/JS -- "Cannot call method 'set' of undefined"

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??

Categories