Parse Connection Times out only for one specific function - javascript

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();

Related

What is the correct way/can you chain two AngularJs service calls and then perform a function with the returned data?

I would like to chain these two service calls, and with the results perform a forEach loop to filter my data, but get a TypeError: "SocialMediaUserService.channelProfiles is not a function" message in Chrome.
However, this works in IE, and no warning or message. :)
function getChannelProfiles() {
GetUserAccessService.returnBrandProfileID().then(function (brandProfileID) {
SocialMediaUserService.channelProfiles().then(function (channelProfiles) {
channelProfiles.forEach(function (channel) {
if (channel.brand_profile_id === brandProfileID && channel.channel_type === 'facebook') {
$scope.facebookChannels.push(channel.channel_url);
console.log($scope.facebookChannels);
}
});
});
});
}
EDIT: This is my SocialMediaUserService.channelProfiles service call:
this.channelProfiles = function () {
var channelProfiles = pullSocialMediaData('list_channel_profiles.json');
console.log("Channel Profiles Logged: " + channelProfiles);
return channelProfiles;
}
This is my SocialMediaUserService.returnBrandProfileID service call:
this.returnBrandProfileID = function () {
var brandProfileID = $q.defer();
if (angular.isUndefined($sessionStorage.brandProfileID)) {
GetDataService.getItems('GetUserAccess/' + $cookies.get('authenticationID'))
.success(function (accessObject) {
brandProfileID.resolve(accessObject.FusewareID);
})
.error(function (error, status) {
console.error('Fuseware API error: ' + error + ' Status message: ' + status);
});
}
else {
brandProfileID.resolve($sessionStorage.brandProfileID);
}
return brandProfileID.promise;
};
Edit 2: This is the pullSocialMediaData function:
function pullSocialMediaData(url) {
var userData = $q.defer();
GetFusionDataService.getItems(url)
.success(function (data) {
userData.resolve(data);
})
.error(function (error, status) {
});
return userData.promise;
}
Thank you!
The SocialMediaUserService.channelProfiles might be designed like this :
this.channelProfiles = function () {
var channelProfilesPromise = new Promise(function (resolve, reject){
pullSocialMediaData('list_channel_profiles.json').then(function(result){
console.log("Channel Profiles Logged: " + result);
resolve(result);
});
});
return channelProfilesPromise
};

Parse-server Cloud Code Function was not called

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)
}
}

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");
}
}
});
});
}

Parse Cloud Code query after save don't works

After saving an array of objects, I do a query for count the number of elements of a class, but the code doesn't run.
Parse.Cloud.define("saveItem", function(request, response) {
Parse.Cloud.useMasterKey();
... (Updating objects...)
Parse.Object.saveAll([item, activity], {
success: function(list) {
response.success("saved"); // <--- THE OBJECTS ARE SAVED, ALLRIGHT
var query = new Parse.Query("Item"); // <--- FROM HERE
query.count({
success: function(count) {
console.log("inside count"); // <--- NOT ENTER HERE!!
},
error: function(error) {
// The request failed
}
});
},
error: function(error) {
response.error(error);
},
});
You need to complete your operations before calling response.success("saved")
Calling response.success is effectively killing the rest of your code.
Parse.Cloud.define("saveItem", function(request, response) {
Parse.Cloud.useMasterKey();
//... (Updating objects...)
Parse.Object.saveAll([item, activity], {
success: function (list) {
var query = new Parse.Query("Item");
query.count({
success: function (count) {
response.success(count);
},
error: function (error) {
// The request failed
response.error("Unable to count items...");
}
});
},
error: function (error) {
response.error(error);
},
});
});

Parse, Function only works for the current user

The following function seems to only work for my Current user, any other ObjectId that I type into the input will return a Post (Bad Request) error in the console.
var query = new Parse.Query(Parse.User);
var userInput = $('#inputObject').val();
query.equalTo("objectId", userInput);
query.first({
success: function(result) {
console.log(result.id);
result.set('money', 20);
result.save();
},
error: function(error) {
console.log("None found.");
}
});
The problem with this is that the Parse user object is only modifiable by the current user. You can get around this by creating a Cloud Code function, which modifies the money value for the intended user. Note, the cloud code function must call Parse.Cloud.useMasterKey(); Source
CloudCode Function:
Parse.Cloud.define("setMoney", function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("objectId", request.params.userId);
query.first({
success: function (result) {
console.log(result.id);
result.set('money', 20);
result.save();
response.success();
},
error: function (error) {
console.log("None found.");
response.error(error);
}
});
});
JavaScript Call
Parse.Cloud.run("setMoney", {userId: userInput}, {
success: function(result) {
// Success
},
error: function(error) {
// Error
}
});

Categories

Resources