Parse-server Cloud Code Function was not called - javascript

I am receiving the error [Error]: Invalid function. (Code: 141, Version: 1.12.0) while calling this Cloud Code function in parse.
Parse.Cloud.define("AddFriendRequest", function (request, response) {
var FriendRequest = Parse.Object.extend("FriendsIncoming");
var FRequest = new FriendRequest();
var user = request.user;
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.find({
success: function (people) {
if(people.length == 0)
{
response.success(-5);
return;
}
var person = people[0];
FRequest.set("OwnerID", user.id);
FRequest.set("TargetFriend", person.id);
FRequest.set("Status", 0);
var query = new Parse.Query("FriendsIncoming");
query.equalTo("OwnerID", user.id);
query.equalTo("TargetFriendID", person.id);
query.find({
success: function (results) {
if (results.length > 0) {
response.success(1);
return;
}
FRequest.save(null, {
success: function (Friend) {
response.success(2);
},
error: function (Friend, error) {
response.error(3);
}
});
response.error(-2);
},
error: function () {
response.error(-1);
}
});
}
,
error: function (Friend, error) {
response.error(-4);
}
});
});
I am calling it using the below code in swift: I did not write the Javascript myself so there may be an obvious issue but there is nothing I can tell from glancing at it with no prior knowledge to JS.
var name : NSString
name = "kooshesh"
//let parameters : [NSObject : AnyObject]
let parameters = ["TargetFriendID" : name]
PFCloud.callFunctionInBackground("AddFriendRequest", withParameters: parameters) { results, error in
if error != nil {
// Your error handling here
} else {
print(results)
}
}

Related

Parse Connection Times out only for one specific function

Below is my cloud code in which I am attempting to use to create a Friend Database, Both the first and the third function work fine however the AcceptFriendRequest function times out as if there is no server there with the error code 100. I am very lost as this happens 100% of the time. All help is appreciated.
Parse.Cloud.define("AddFriendRequest", function (request, response) {
var FriendRequest = Parse.Object.extend("FriendsIncoming");
var FRequest = new FriendRequest();
var user = request.user;
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.find({
success: function (people) {
if(people.length == 0)
{
response.success(-5);
return;
}
var person = people[0];
FRequest.set("OwnerID", user.id);
FRequest.set("TargetFriend", person.id);
FRequest.set("Status", 0);
var query = new Parse.Query("FriendsIncoming");
query.equalTo("OwnerID", user.id);
query.equalTo("TargetFriendID", person.id);
query.find({
success: function (results) {
if (results.length > 0) {
response.success(1);
return;
}
FRequest.save(null, {
success: function (Friend) {
response.success(2);
},
error: function (Friend, error) {
response.error(3);
}
});
response.error(-2);
},
error: function () {
response.error(-1);
}
});
}
,
error: function (Friend, error) {
response.error(-4);
}
});
});
Parse.Cloud.define("AcceptFriendRequest", function (request, response) {
var user = request.user;
var query = new Parse.Query("FriendsIncoming");
query.equalTo("OwnerID", user.id);
query.equalTo("TargetFriendID", request.params.TargetFriendID);
query.find({
success: function (results) {
if (results.length > 0) {
response.success(1);
return;
}
FRequest.save(null, {
success: function (Friend) {
response.success(2);
},
error: function (Friend, error) {
response.error(3);
}
});
response.error(-2);
},
error: function () {
response.error(-1);
}
});
});
Parse.Cloud.define("RetrieveFriends", function (request, response) {
var query = new Parse.Query("FriendsAccepted");
var results = [];
query.find().then(function (Friends) {
for (var i = 0; i < Friends.length; i++) {
results.push(Friends[i]);
}
// success has been moved inside the callback for query.find()
response.success(results);
}, function (error) {
// Make sure to catch any errors, otherwise you may see a "success/error not called" error in Cloud Code.
response.error("Could not retrieve Posts, error " + error.code + ": " + error.message);
});
});
It seems like the FRequest in the AcceptFriendRequest is not initialized...
add this in the inside of the function. The timeout on parse.com is 15 seconds for cloud function and for afterSave 3 secs. They might changed it though cause they are shutting down
var FriendRequest = Parse.Object.extend("FriendsIncoming");
var FRequest = new FriendRequest();

Parse TypeError cannot read property 'entry' of undefined at main.js at 39:27

I have a swift project in which i want use push notifications. I tried use parse server using a job schedule with .js files. The problem is that when i run the job on job status window i get this error:
"TypeError cannot read property 'entry' of undefined at main.js at 39:27"
Here is my main.js file:
var xmlreader = require('cloud/xmlreader.js');
var url = "http://www.ilsecoloxix.it/homepage/rss/homepage.xml";
function SavePost(title, link){
var PostClass = Parse.Object.extend("Post");
var post = new PostClass();
post.set("title", title);
post.set("link", link);
post.save();
}
function SendPush(title, link){
var query = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: query,
data: {
url: link,
alert: title,
sound: "default"
}
}, {
success: function() {
SavePost(title, link);
response.success("Push sent to everyone");
},
error: function(error) {
response.error("Error sending push: "+error);
}
});
}
Parse.Cloud.job("fetchPosts", function(request, response) {
Parse.Cloud.httpRequest({
url: url,
success: function(httpResponse) {
xmlreader.read(httpResponse.text, function (err, res){
var newPost = res.feed.entry.at(0);
var title = newPost.title.text();
var link = "";
newPost.link.each(function (i, linkObj){
if (linkObj.attributes().rel == "alternate"){
link = linkObj.attributes().href;
}
});
var PostClass = Parse.Object.extend("Post");
var query = new Parse.Query(PostClass);
query.equalTo("link", link);
query.find({
success: function(results) {
console.log(results);
if (results.length == 0){
SendPush(title, link);
} else {
response.error("Post already pushed");
}
}
});
});
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error("Error fetching posts from feed");
}
});
});
How can i avoid this problem?
I am using Fiddler to display xml and find no feed node, should be res.rss.channel
http://www.ilsecoloxix.it/homepage/rss/homepage.xml
Response data res.feed is undefined. You can console.log(res) to figure out what is wrong. Also make sure the signature is correct for xmlreader.read(httpResponse.text, function (err, res), not sure why err is the first parameter of the function, most likely res is the first.
if(res && res.feed && res.feed.entry){
var newPost = res.feed.entry.at(0);
var title = newPost.title.text();
var link = "";
newPost.link.each(function (i, linkObj){
if (linkObj.attributes().rel == "alternate"){
link = linkObj.attributes().href;
}
});
var PostClass = Parse.Object.extend("Post");
var query = new Parse.Query(PostClass);
query.equalTo("link", link);
query.find({
success: function(results) {
console.log(results);
if (results.length == 0){
SendPush(title, link);
} else {
response.error("Post already pushed");
}
}
});
});
}

How to use Promises/Chaining with Azure Mobile Services Custom API in Javascript

I am trying to figure out how to use Promises with the AMS Javascript API.
These are the two functions I have created that will be 'Promised'
function checkUsername(username, table) {
return table.where({username: username}).read({
success: function (results) {
if (results.length === 0) {
return true;
} else {
return false;
}
},
error: function(error) {
return false;
}
});
}
function checkEmail(email, table) {
return table.where({email: email}).read({
success: function (results) {
if (results.length === 0) {
return true;
} else {
return false;
}
},
error: function(error) {
return false;
}
});
}
checkUsername(body.username, accountsTable).then(function (results) {
if (results) {
return checkEmail(body.email, accountsTable);
} else {
response.send(400, {message: 'This username is already in use.'});
}
}).then(function(results) {
if (results) {
response.send(200, {message: 'Can proceed with sign up.'});
} else {
response.send(400, {message: 'This email address is already in use.'});
}
});
I am trying to use the promises as I would in Parse, but it's clearly not working. The console logs keep spitting out an Internal Server Error and that .then() is not a function of the object. I'm assuming I am missing a require or something in order to have the Promises functionality?
Error in script '/api/register.js'. TypeError: Cannot call method 'done' of undefined
at exports.post (D:\home\site\wwwroot\App_Data\config\scripts\api\register.js:30:59)
[external code]
I have realized what I was doing wrong.
I have now decided to use the Q Node Module for my promises.
var q = require('q');
exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
// var push = request.service.push;
var tables = request.service.tables;
var accountsTable = tables.getTable('Accounts');
var params = request.body;
checkUsername(params.username, accountsTable).then(function (result) {
if (result.length === 0) {
return checkEmail(params.email, accountsTable);
} else {
response.send(400, {message: 'This username is in use.'});
}
}).then(function (results) {
if (results.length === 0) {
return;
} else {
response.send(400, {message: 'This email address is already registered.'});
}
}).then(function () {
response.send(200, {message: 'Username and email are unique. You can register!'});
});
};
function checkUsername(username, table) {
var deferred = q.defer();
table.where({username: username}).read({
success: function (result) {
deferred.resolve(result);
},
error: function (error) {
deferred.reject(error);
}
});
return deferred.promise;
}
function checkEmail(email, table) {
var deferred = q.defer();
table.where({email: email}).read({
success: function (result) {
deferred.resolve(result);
},
error: function (error) {
deferred.reject(error);
}
});
return deferred.promise;
}

Uncaught SyntaxError: Unexpected token using an else statement in JavaScript code block

Why do I get a "Uncaught SyntaxError: Unexpected token" else eror in Chrome tools using this code, I thought I should just use else here not else if ?
var friendName;
function findFriend(){
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function(friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
}
else {
// Query executed with success
{ alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
$('#find_button').click(function(e){
findFriend();
});
With reasonable (auto-)indentation, you spot the error(s) easily:
function findFriend() {
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function (friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
} else {
// Query executed with success
{
alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
The braces around the else are too much, it should be
function findFriend() {
friendName = $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(Parse.User);
query.equalTo("username", friendName); // find users that match
query.find({
success: function (friendMatches) {
if (friendMatches.length === 0)
alert('NO MATCH FOUND!!!');
else // Query executed with success
alert('MATCH FOUND!!!');
},
error: function (error) {
alert('query failed with error' + error.message);
}
});
}
or
…
success: function (friendMatches) {
if (friendMatches.length === 0) {
alert('NO MATCH FOUND!!!');
} else {
// Query executed with success
alert('MATCH FOUND!!!');
}
},
…
change
{ alert('MATCH FOUND!!!');
to
alert('MATCH FOUND!!!'); }

Node js custom callback function error

I'm trying to make a simple authentication with node js. Because I read user data from a database, I have to make it asynchronous. Here's my function, which checks if authentication is ok:
function auth(req, callback) {
var header = req.headers['authorization'];
console.log(cb.type);
console.log("Authorization Header is: ", header);
if(!header) {
callback(false);
}
else if(header) {
var tmp = header.split(' ');
var buf = new Buffer(tmp[1], 'base64');
var plain_auth = buf.toString();
console.log("Decoded Authorization ", plain_auth);
var creds = plain_auth.split(':');
var name = creds[0];
var password = creds[1];
User.findOne({name:name, password:password}, function(err, user) {
if (user){
callback(true);
}else {
callback(false);
}
});
}
}
And here I call the function:
auth (req, function (success){
if (!success){
res.setHeader('WWW-Authenticate', 'Basic realm="myRealm');
res.status(401).send("Unauthorized");
}else{
if(user!==req.user) {
res.status(403).send("Unauthorized");
}else{
User.findOneAndUpdate({user:userid}, {user:req.body.user, name:req.body.name, email:req.user.email, password:User.generateHash(req.body.password)},
{upsert:true}, function(err, user) {
if(!err) {
res.status(200).send("OK");
}else{
res.status(400).send("Error");
}
});
}
}
});
This gives me error "TypeError: object is not a function", pointing at "callback(false)". I have no idea what could cause this error, as I pass a function as a parameter, and the first log message prints "[function]". Any help would be appreciated.

Categories

Resources