Parse.com Cloud Code Failing - parse-platform

I am trying to use the following cloud code
Parse.Cloud.job("sendAlert", function(sendAlert, status) {
Parse.Push.send({
data: {
"content-available": 1,
}
}, {
success: function() {
status.success("Push Worked!!!");
},
error: function(error) {
status.error("Uh oh, something went wrong.");
}
});
});
to send a silent push alert.
It fails and calls the error function. What is the issue?

Allow me to answer my own question, I got the following code to work.
var query = new Parse.Query(Parse.Installation);
Parse.Cloud.job("sendAlert", function(sendAlert, status) {
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "Broadcast to everyone"
}
}, {
success: function() {
status.success("Push Worked!!!");
},
error: function(error) {
status.error("Uh oh, something went wrong.")
}
});
});

Related

Kendo DataSource catch server response

In my kendo dataSource > transport > update. I want to catch a server response status (refer image), but none of this methods trigger an alert. Any idea why?
update: {
url: "./getRevenueAccounts.php",
type: "POST",
data: function() {
return {
method: "editRevenueAccounts"
}
},
success: function(e) {
if(e.status == 'duplicate'){
alert('Trigger 1');
}
},
error: function(e) {
if (e.errorThrown == 'duplicate') {
alert("Trigger 2");
}else if(e.status == 'duplicate' ){
alert("Trigger 3")
}
},
complete: function (e) {
if(e.status == 'duplicate'){
alert('Trigger 4');
}
}
},
console.log(e) screen shot
Try the following code for your success function:
success: function(e) {
if(e.responseText.status == 'duplicate'){
alert('Trigger 1');
}
},
Essentially, you are looking at the status property when you should have been looking at the responseText property to get the status (which is another property on that object).
You need to make an ajax call inside the update function.
Like:
var dataSource = new kendo.data.DataSource({
transport: {
read: function(options) {
/* implementation omitted for brevity */
},
update: function(options) {
// make JSONP request to https://demos.telerik.com/kendo-ui/service/products/update
$.ajax({
url: "https://demos.telerik.com/kendo-ui/service/products/update",
dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
// send the updated data items as the "models" service parameter encoded in JSON
data: {
models: kendo.stringify(options.data.models)
},
success: function(result) {
// notify the data source that the request succeeded
options.success(result);
},
error: function(result) {
// notify the data source that the request failed
options.error(result);
}
});
}
},
batch: true,
schema: {
model: { id: "ProductID" }
}
});
For more details please check this from telerik documentation: https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/configuration/transport.update
Is not a good method to apply, but it works to fetch the response.
if(e.responseText=='{"status":"duplicate"}'){
kendo.alert('duplicate data');
}

Parse Cloud job

I have a job in the parse cloud, inside the job I've a Parse.Cloud.run, when I run this function works fine and parse data base is update, but in the in the cloud job statuses appears failed. Here's my code:
Thanks in advance.
Parse.Cloud.job("updateTopsThreeJob", function(request, status) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query("_User");
query.descending("followersOfMe");
query.limit(3);
query.find({
success: function(results) {
var TestJS = Parse.Object.extend("testJS");
var test = new TestJS();
var listTops = [];
for (var i = 0; i < results.length; i++) {
var object = results[i].get("username");
listTops.push(object);
}
Parse.Cloud.run("updateTopsThree", {objects: listTops}, {
success: function(result) {
status.success("Migration completed successfully.");
response.success(result)
},
error: function(error) {
status.error("Uh oh, something went wrong.");
}
});
response.success(listTops);
},
error: function(error) {
response.error("failed");
}
});
});
Parse.Cloud.define("updateTopsThree", function(request, response) {
var tops = Parse.Object.extend("testJS");
var query = new Parse.Query(tops);
query.get(ObjIDs.topsThreeID(), {
success: function(topsThree) {
topsThree.set("topsThree", request.params.objects);
topsThree.save();
response.success(topsThree);
},
error: function(object, error) {
response.error(error);
}
});
});
The Parse Cloud Code runs much like any other javascript file. In order to declare another function to be called within the parse .js file, such as in this case, you do not need to define the function using Parse syntax. Define and call it just as you would a normal Javascript function.
Use this to call the function within your Parse.job:
updateTopsThree(topThreeObjects);
Define function:
function updateTopsThree(topObjects) {
var tops = Parse.Object.extend("testJS");
var query = new Parse.Query(tops);
query.get(ObjIDs.topsThreeID(), {
success: function(topsThree) {
topsThree.set("topsThree", topObjects);
topsThree.save();
response.success(topsThree);
},
error: function(object, error) {
response.error(error);
}
});
}
Thanks, but finally I´ve solved my problem as follows: I´ve created a cloud function like this:
Parse.Cloud.define("setLikesInDB", function(request, response) {
var query = new Parse.Query("testJS");
query.get(ObjIDs.topsLikesID(), {
success: function(topsThree) {
topsThree.set("topsLikes", "likes");
topsThree.save();
response.success(topsThree)
},
error: function(object, error) {
response.error(error);
}
});
});
And then into my Parse.Cloud.Job I´ve called a cloud function like this:
Parse.Cloud.run('setLikesInDB', {obj : listTops}, {
success: function(result) {
response.success(result);
},
error: function(error) {
response.error('some error')
}
});
This way works fine.
I hope this helps someone else.

Meteor Store Image URL as Avatar

I have an image uploader that uses amazon s3 to store files. I am trying to update my users.avatar value to the amazon url upon upload.
Here is my upload event:
Template.avatarUpload.events({
"click button.upload": function(){
event.preventDefault();
var files = $("input.avatar-upload")[0].files
S3.upload({
files: files,
path: "avatars"
}, function(e,r) {
console.log(r);
});
}
})
I tried something like the following based on a few other stackoverflow QA's:
First this, but then I ran into a problem where I couldn't store files.url without getting an error (update failed: MongoError: '$set' is empty. You must specify a field like so: {$mod: {: ...}})
Meteor.users.update({_id:Meteor.user()._id}, {$set:{"profile.name": files.url}})
Then like this:
var set = {avatar: {}};
set.avatar[files.url];
Meteor.users.update({
_id: Meteor.user()._id
}, {$set: set});
But no luck. Basically trying to take the URL and store it in users.avatar. My json looks like this:
users = [{
username: "normcore",
avatar: "avatar_url"
}]
Post the whole code for the helper.
Overall, this is something your code should look like:
Template.avatarUpload.events({
"click button.upload": function(){
event.preventDefault();
var files = $("input.avatar-upload")[0].files
S3.upload({
files: files,
path: "avatars"
}, function(e,r) {
if(e) {
console.log(r);
} else if(r) {
console.log(r);
Meteor.users.update(Meteor.user(), {$set: {avatar: r.url}}); // Correct this to actual URL in response. If multiple files
}
});
},
})
You'll have to figure out how to handle the case of multiple files upload. One way is to not enable multi in your input tag.
This is how we got it working. I'll leave this here for anyone having the same issue:
Client side:
Template.avatarUpload.events({
"click button.upload": function(){
event.preventDefault();
var files = $("input.avatar-upload")[0].files
S3.upload({
files: files,
path: "avatars"
}, function(e,r) {
console.log(r);
console.log(Meteor.user());
var user = Meteor.user();
var student = {"_id": user._id };
console.log(student);
var url = r.secure_url;
Meteor.call('updateAvatar', student, url, function(error) {
if (error) {
return console.log(error);
} else {
return console.log('success');
}
});
});
}
});
Template.avatarUpload.helpers({
"files": function(){
return S3.collection.find();
}
});
Server Side:
Meteor.methods({
updateAvatar: function(user, url) {
console.log(user);
check(user, {
_id: String
});
check(url, String);
return Meteor.users.update({_id:Meteor.user()._id},
{
$set: {
"profile.avatar": url
}
}, function(error) {
if (error) {
return console.log(error);
}
});
}
});

Unable to send Push notification to Installation via Parse CloudCode

I'm attempting to send a push notification to a specific user's installation.
In the user class I have a column which is a pointer to an installation instance. Here's my code:
Parse.Cloud.beforeSave("Notification", function(request, response) {
var user;
if (!request.object.get("reciever")) {
response.error("Please include a user");
} else {
user = request.object.get("reciever");
}
var message = request.object.get("message");
var query = new Parse.Query(Parse.User);
query.include("installation");
query.equalTo("objectID", user.get("objectID"));
query.find({
success: function(result) {
var obj = result[0];
var intst = obj.get("installation");
console.log(intst);
Parse.Push.send({
where: intst,
data: {
alert: message
}
}, {
success: function() {
console.log("Push was successful");
response.success();
},
error: function(error) {
console.error(error);
response.error(error);
}
});
},
error: function(error) {
response.error(error);
}
});
});
From looking at my logs, the installation instance was received correctly. When sending, I get the following error:
Failed to create new object, with error code:
{"code":115,"message":"Missing the push channels."}
What am I doing wrong?
The Parse documentation specifys this as an example for sending Push notificatons using Javascript
Parse.Push.send({
channels: [ "Giants", "Mets" ],
data: { alert: "The Giants won against the Mets 2-3." }
}, {
success: function() {
// Push was successful
}, error: function(error) {
// Handle error
}
});
What you are missing is the 'channels: ["Giants", "Mets"]' section of this push notifcation. If you go into your installation table in parse you will notice there is a Channels column and this is what defines who the pushes get sent to. In this example the push will go to anyone who has signed up for the channels 'Giants' and 'Mets'.

How do I trap a requestexception in an Ajax request in Sencha Touch 2?

In my Sencha Touch app Ext.device.Connection.isOnline() will return true if the device is connected to wifi but doesn't have an internet connection - so my Ajax calls hang. I'm trying to trap the requestexception event so I can handle the error gracefully, but can't get it to work. Here's the code:
Ext.Ajax.request({
url: [my url],
jsonData:{ },
params:{
op: 'myop'
},
method:"POST",
success:function(response, opts){
// all good
},
failure:function(response, opts){
// failed
},
listeners: {
requestexception: function(connection, response, options, eOpts) {
switch(response.status) {
case 0 :
Ext.Msg.alert('Error Loading', 'No Internet connection.');
break;
default :
Ext.Msg.alert('Whoops!', 'An unexpected error has occurred.');
}
}
}
});
Not sure what I'm doing wrong!
Thanks
Matt
Ext.define('MyApp.store.MyJsonStore',
extend: 'Ext.data.Store',
config: {
storeId: 'MyJsonStore',
proxy: {
type: 'ajax',
reader: {
type: 'json'
}
}
},
constructor: function() {
var me = this;
me.callParent(arguments);
me.getProxy().on([{
fn: 'onAjaxproxyException',
event: 'exception',
scope: me
}]);
},
onAjaxproxyException: function(server, response, operation, options) {
//Your code here
}
});

Resources