Parse,User.signUp error - javascript

The code below creates a user in Parse but always returns and error with meaningless error info.
var user = new Parse.User();
user.set("username", "jaskaye17"); // in my app, email==username
user.set("password", "1234");
user.set("email", "exmaple#gmail.com");
user.signUp(null, {
success: function(user) {
alert('Successfully created user' + user);
},
error: function(user, error) {
alert("Error message " + error.message + '; Error code ' + error.code);
}
});
The user is create in the user table but,
user.signUp returns an error
The error message is nonsensical
e.Error
code: -1
message: ""
__proto__: Object
What is code: -1 and why is message "". Shouldn't there be more information about what the error is?

I think you are using IE, try use firefox or chrome to see the actual message

Related

Back4App: Parse / JS error "Unhandled Promise Rejection: SecurityError: The operation is insecure."

when I try to create a new User with JavaScript and Parse in Back4App.io I receive the following error:
Unhandled Promise Rejection: SecurityError: The operation is insecure.
I use the following code:
Parse.initialize("APP_ID", "JS_KEY"); //PASTE HERE YOUR Back4App APPLICATION ID AND YOUR JavaScript KEY
Parse.serverURL = "https://parseapi.back4app.com/";
var user = new Parse.User();
user.save({
username: 'Taki Test',
email: 'sample#email.com',
password: '123456'
}, {
success: function(response) {
alert('New object create with success! ObjectId: ' + response.id + `, ` + user.get('username'));
},
error: function(response, error) {
alert('Error: ' + error.message);
}
})
It seems that you didn't insert your master key in your initialization code, and that was the reason you're facing this trouble.
I tested your code with master key and it worked for me.
Also, on the "API Reference" available in the Dashboard, on "User API" > Signing Up, there's a simple code that you only need to run and it will create an user too, here it is:
Parse.serverURL = 'https://parseapi.back4app.com';
Parse.initialize('appId', 'jskey', 'masterkey');
const user = new Parse.User()
user.set('username', 'A string');
user.set('email', 'email#email.com');
user.set('password', '123456');
user.signUp().then((user) => {
if (typeof document !== 'undefined') document.write(`User signed up: ${JSON.stringify(user)}`);
console.log('User signed up', user);
}).catch(error => {
if (typeof document !== 'undefined') document.write(`Error while signing up user: ${JSON.stringify(error)}`);
console.error('Error while signing up user', error);
});

ParseError: 'bad or missing username'

So I have some cloud code I am trying to write to like a post.
My database is setup that users have a likedPosts array, which has object id's of all the posts that the user liked. Users also have a column coins, that should get incremented when users like their posts.
The post object has a likes column which is an integer that gets incremented with each like, and the post object also has a posterId column, which is the object id of the user that posted it.
Here is my function right now (I am not very good at javascript and cloud code, so if there is something horribly wrong, I'm sorry)
Parse.Cloud.define("likePost", function(request, response) {
Parse.Cloud.useMasterKey();
var senderId = request.params.senderId;
var postId = request.params.postId;
var post = new Parse.Object ({objectId: postId});
var posterId = post.posterId
var poster = new Parse.User ({objectId: posterId});
var sender = new Parse.User ({objectId: senderId});
sender.add("likedPosts", postId);
poster.increment("coins");
post.increment("likes");
poster.save(null, {useMasterKey:true, success:
function(poster) {
console.log("Successfully saved poster");
}, error: function(poster, error) {
console.error("Error saving poster: " + error.message);
response.error(error);
}
});
post.save(null,{useMasterKey:true, success:
function(post) {
console.log("Successfully saved post");
}, error: function(post, error) {
console.error("Error saving post: " + error.message);
response.error(error);
}
});
sender.save(null, {useMasterKey:true, success:
function(sender) {
console.log("Successfully saved sender");
}, error: function(sender, error) {
console.error("Error saving sender: " + error.message);
response.error(error);
}
});
response.success();
});
I call the function from swift like so:
PFCloud.callFunction(inBackground: "likePost", withParameters: ["senderId" : PFUser.current()!.objectId!, " postId": postObject!.objectId!], block: { (result, error) in
if (error != nil) {
print(error!)
} else {
print("success liking")
}
})
In my logs, however, I get the following error:
2017-06-21T21:47:59.499Z - Failed running cloud function likePost for user R4d8Zrcdhw with:
Input: {"senderId":"R4d8Zrcdhw"," postId":"XXbu55PdpR"}
Error: {"code":141,"message":{"code":200,"message":"bad or missing username"}}
2017-06-21T21:47:59.492Z - bad or missing username
My guess is that the request is missing a header to define the content-type. I've seen Parse return the "bad or missing username" error via the Parse REST API if the Swift URLSession was using an incorrect content-type header.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
or
Parse.Cloud.httpRequest({
url: 'http://www.example.com/',
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
})

Parse Cloud Code Error - 'Master Key is Required'?

Whenever I try to run this snippet of cloud code, I receive an error stating that:
Error generating response. ParseError {
code: 141, message: 'Push failed to send with error: master key is required'}
I've tried to follow some of the other solutions on the site such as using Parse.Cloud.useMasterKey() & useMasterKey: true but I haven't found success with any of these commands (possibly due to me using them incorrectly?).
Parse.Cloud.define("sendPushToUser", function(request, response) {
var senderUser = request.user;
var recipientUserId = request.params.recipientId;
var message = request.params.message;
var recipientUser = new Parse.User();
recipientUser.id = recipientUserId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
});
Swift function:
func testPush() {
PFCloud.callFunction(inBackground: "sendPushToUser", withParameters: ["recipientId": PFUser.current()?.objectId!, "message" : "Test notification"]) { (success, error) in
if error != nil {
print("error occurred")
}else {
print("Sent successfully")
}
}
}
As Gellert Lee suggested
Did you configure your masterKey in your index.js? masterKey : process.env.MASTER_KEY ||'your masterkey'

Signup using parse api (401 (Unauthorized))

I am trying to signup the user on parse server.I have initialize the parse object to applicationid and key, but I am not able to signup.I am getting unauthorize error.I am working on chrome app. i also allow the permission in mainifest file.
Parse.initialize("app_id", "key");
var username= "jitendra.singh#gmail.com";
var password = "singh";
Parse.User.signUp(username, password, {}, {
success: function (user) {
console.log("Yay!");
},
error: function (user, error) {
console.log("Error: " + error.code + " " + error.message);
}
});
Error:
POST https://api.parse.com/1/users 401 (Unauthorized)
init your parse
Parse.initialize("app_id", "key");
call this function
function Signup(userInfo){
var user = new Parse.User();
// set the properties of the user
user.set("username", "username");
user.set("password", "password");
user.set("email", "emailid");
// Create a custom field for the user (there is no limit to the number of custom records)
user.set("score", 0);
user.signUp(null, {
success: function(user) {
// return the success response
console.log("Success!");
},
error: function(user, error) {
// return the error response
console.log("Error: " + error.code + " " + error.message);
}
});
}
If you are using the parse-server then you need to change the initialize
JavaScript
Parse.initialize("YOUR_APP_ID");
Parse.serverURL = 'http://localhost:1337/parse'
See https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide#migrating

Cloud Code Not Updating User's Username Properly

For some reason Cloud Code isn't updating the current user's username, even though it is updating the email field. I'm using the master key, and although everything returns success, the username doesn't update (even on the data browser).
Here's my code:
//Get Current User
var user = Parse.User.current();
//Update username
user.set("email", request.params.email);
user.set("username", request.params.username);
user.save(null, {
//Success
success: function(user) {
//Done
response.success("Username saved! 🎉");
},
//Error
error: function(user, error) {
// Execute any logic that should take place if the save fails.
response.error("Aww man. Something went wrong. Please try again. 😅");
}
});
I've made sure that the parameters are being passed correctly, and that there isn't a mistake with the name etc on my iOS app.
My guess is that there is an issue with getting the calling user.
Use request.user to get the calling user and try the following.
// Get the requesting user
var user = request.user;
if (user) {
user.set("email", request.params.email);
user.set("username", request.params.username);
user.save(null, {
success: function(user) {
response.success("Username saved! 🎉");
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
} else {
response.error("Aww man. Something went wrong. Please try again. 😅");
}

Categories

Resources