Parse: using httpRequest to load image - javascript

I'm trying to load an image using Parse.Cloud.httpRequest promise inside of one beforeSave trigger, but apparently this promisse is not called. The error callback is called, but the error message is always empty.
Is possible do it into a beforesave trigger? Or maybe it's happening because of that i'm doing this inside of foreach?
The message "pablo# after load imageUrl" don't appears in Parses log, and the message "error when save user picture>>>" appears, but the error.text is empty
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
Parse.Cloud.useMasterKey();
response.success();
var found = false;
console.log(request.object.dirtyKeys());
for (dirtyKey in request.object.dirtyKeys()) {
if (request.object.dirtyKeys()[dirtyKey] === "imageurl") {
found = true;
Parse.Cloud.httpRequest({
url: request.object.get("imageurl")
}).then(function(response) {
console.log("pablo# after load imageUrl");
var image = new Image();
return image.setData(response.buffer);
}).then(function(image) {
return image.data();
}).then(function(buffer) {
var base64 = buffer.toString("base64");
var fileTitle = request.object.id + ".png";
console.log(fileTitle);
var file = new Parse.File(String(fileTitle), { base64: base64 });
return file.save();
}).then(function(file) {
request.object.set("profileImage", file);
console.log('success');
response.success();
}, function(error) {
console.error(error);
response.error("error when save user picture>>> " + error.text);
});
}
}
if(!found){
response.success();
}
});

This is all doable with just a few improvements to the code: (1) don't call success() straight away or nothing at all will happen, (2) no need to loop dirty keys, since we're just checking for existence of one of them, (3) your code assumes that image.setData() returns a promise fulfilled by the image data: the docs are unclear about this but imply otherwise, (4) finally, let's factor the image stuff into a single, promise returning function so its easier to see what's happening...
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
fetchAndSaveUserImage(request.object).then(function() {
response.success();
}, function(error) {
response.error(error);
});
}
function fetchAndSaveUserImage(user) {
if (user.dirtyKeys().indexOf("imageurl") == -1) { return false; }
var image = new Image(); // more like docs, so we have the image in any block below
var params = { url: request.object.get("imageurl") };
return Parse.Cloud.httpRequest(params).then(function(response) {
console.log("pablo# after load imageUrl");
return image.setData(response.buffer);
}).then(function() {
return image.data(); // this is likely the same as response.buffer, but the docs do it, so just to be certain...
}).then(function(buffer) {
var base64 = buffer.toString("base64");
var fileTitle = user.id + ".png";
console.log(fileTitle);
var file = new Parse.File(String(fileTitle), { base64: base64 });
return file.save();
}).then(function(file) {
request.object.set("profileImage", file);
return true;
});
}
Incidentally, I omitted useMasterKey since I saw nothing in the code needing more permission than what's already assumed in beforeSave of a User.

Related

Beacon tracking image with 204 response & callback function

I have been trying for a few days now to convert my tracking pixel JS functionality to use a 204 "no_content" response.
I can easily get this working, but I need to be able to fire a callback function afterwards.
The following doesn't seem to ever get fired once the 204 is returned.
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
error: function () {
console.log('error');
},
success: function () {
console.log('success');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
if (beacon.onerror) {
beacon.onerror = opts.error;
}
if (beacon.onload) {
beacon.onload = opts.success;
}
$(beacon).on('load', function( response, status, xhr ){
alert(status);
});
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
The tracking is logged properly, but no alert or console log messages. Is this possible or am I just wasting my time?
Edit ------
Based on the solution below here is the final version (this assumes that both an error & success will use the same callback.
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
callback: function () {
console.log('tracked');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
$(beacon).on('error success done', opts.callback);
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
You aren't attaching any callbacks to image. Your test if (beacon.onerror) results in false because beacon.onerror is null.
You should use if( "onerror" in beacon ) to tests whether beacon has onerror property.
But why dont you just use jquery's method on?
$(beacon).on("error", function() {
alert("Jquery error");
});
$(beacon).on("done", function() {
alert("Jquery done");
});

Fire second httpRequest after parsing data from first request

I currently have a function which makes a httpRequest and parses the json received into an array of urls. I want to fire a second httpRequest after the first request is complete and data is parse, below both solutions I've tried return null.
Solution1
var promises1 = [];
Parse.Cloud.define("FetchData", function(request, response) {
var promises = _.map(urls, function(url) {
return Parse.Cloud.httpRequest({ url:url });
});
Parse.Promise.when(promises).then(function() {
createSearchUrls(arguments)
//Creates an array of urls from request data to be used in second http request
});
//Fire second HTTP request here after urls have been created from first request data
var promises1 = _.map(appTitles, function(appTitles) {
return Parse.Cloud.httpRequest({ url: appTitles});
});
Parse.Promise.when(promises1).then(function() {
//nothing returned
response.success(_.toArray(arguments));
}, function (error) {
response.error("Error: " + error.code + " " + error.message);
});
});
Solution 2 (Using then after createSearchUrl() function
Parse.Cloud.define("FetchData1", function(request, response) {
var promises = _.map(urls, function(url) {
return Parse.Cloud.httpRequest({ url:url });
});
Parse.Promise.when(promises).then(function() {
//Creates an array of urls from request data to be used in second http request
createSearchUrls(arguments).then( function() {
//Fire second HTTP request here after urls have been created from first request data
promises_1 = _.map(appTitles, function(appTitles) {
return Parse.Cloud.httpRequest({ url: appTitles});
});
})
});
Parse.Promise.when(promises_1).then(function() {
//nothing returned
response.success(_.toArray(arguments));
}, function (error) {
response.error("Error: " + error.code + " " + error.message);
});
});
createSearchUrls()
function createSearchUrls(arguments){
for (a = 0; a < arguments.length; a++){
var json = JSON.parse(arguments[a].text);
for (i = 0; i < json.feed.entry.length; i++) {
var urlEncoded = encodeURI(ENCODE JSON DATA);
var finalUrl = 'URL HERE';
appTitles.push(finalUrl);
}
}
return appTitles;
}
It looks like the idea of making a series of httpRequests and collecting the results is something that can and should be factored out....
function manyRequests(urls) {
var promises = _.map(urls, function(url) {
return Parse.Cloud.httpRequest({ url:url });
});
return Parse.Promise.when(promises).then(function() {
return _.toArray(arguments);
});
}
Now its just a matter of calling that twice....
Parse.Cloud.define("FetchData1", function(request, response) {
manyRequests(urls).then(function(results) {
createSearchUrls(results); // assigns to the gobal "appTitles"
return manyRequests(appTitles);
}).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
});
What that says is, call (a globally defined, presumably) list of urls and collect the results. From those results, run a local function to generate another list of urls (assigning those to a global, presumably), call those and return the result to the client.

Parse Promises Multiple httpRequest Cloud Code

I'm writing an iOs app with Parse.com and Cloud Code. Actually I want to retrieve objects which contain one picture and other informations from a website and I want to add them to a class named News. When I run my code, every object is saved (in my class, one row = one retrieved object) but unfortunately the only first one has its picture saved.... Any idea ?
I made a lot of searches about promises (series / parallels) and I think the problem comes from here..
Note : don't worry about myLink, myImgLink : I put this to make my code easy to read !
Parse.Cloud.define("rajouteNews", function(request, response) {
Parse.Cloud.httpRequest({ url: 'myUrl'}).then(function(httpResponse) {
var news = [];
var NewsClass = Parse.Object.extend("news");
for (var i = 0; i < 10 ; ++i) {
var maNews = new NewsClass();
maNews.set("link", myLink[i]); // "Other informations"
maNews.set("imgLink", myImgLink[i]);
maNews.set("title", myTitle[i]);
var promises = [];
promises.push(Parse.Cloud.httpRequest({
url: $('img').attr('src'),
method: 'GET',
}).then(function(httpResponse){
var imgFile = new Parse.File("photo.jpg", {base64:httpResponse.buffer.toString('base64')});
maNews.set("image",imgFile); // The picture
return maNews.save();
}));
news.push(maNews);
}
promises.push(Parse.Object.saveAll(news, {
success: function (list) {
response.success(news.length.toString() + " ont été sauvegardées");
},
error: function (list, err) {
response.error("Error adding news");
}
}));
return Parse.Promise.when(promises);
}).then(function(bla,result){
response.success("Job done");
}, function(error) {
response.error(error);
}
);
});
Your promises array should put out of the for loop scope. Otherwise , your promise array would be assigned to be a new blank array each loop.
Parse.File would be saved automaticly when its parent do save, you don't need to save it in advance.
So I improve your code as following, try it and tell me weather it works.
Parse.Cloud.define("rajouteNews", function(request, response) {
Parse.Cloud.httpRequest({
url: 'myUrl'
}).then(function(httpResponse) {
var promises = [];
var NewsClass = Parse.Object.extend("news");
for (var i = 0; i < 10; ++i) {
var maNews = new NewsClass();
maNews.set("link", myLink[i]); // "Other informations"
maNews.set("imgLink", myImgLink[i]);
maNews.set("title", myTitle[i]);
var maNewsPromise = Parse.Cloud.httpRequest({
url: $('img').attr('src'),
method: 'GET',
}).then(function(httpResponse) {
var imgFile = new Parse.File("photo.jpg", {
base64: httpResponse.buffer.toString('base64')
});
maNews.set("image", imgFile); // The picture
return maNews.save();
});
promises.push(maNewsPromise);
}
return Parse.Promise.when(promises)
}).then(function(bla, result) {
// this function is call when `Parse.Promise.when(promises)` is done,
//I can't figure out why you take two params.
response.success("Job done");
}, function(error) {
response.error(error);
});
});

How save image in parse cloud using javascript

we are trying to save image in Parse using CloudCode.
we followed this link.
we are new to javascript,plz guide us...!
Thanks in advance.......
var url = file.url();
Parse.Cloud.httpRequest({ url: url }).then(function(response) {
// Create an Image from the data.
var image = new Image();
return image.setData(response.buffer);
}.then(function(image) {
// Scale the image to a certain size.
return image.scale({ width: 64, height: 64 });
}.then(function(image) {
// Get the bytes of the new image.
return image.data();
}.then(function(buffer) {
// Save the bytes to a new file.
var file = new Parse.File("image.jpg", { base64: data.toString("base64"); });
return file.save();
});
we getting error like this
There's a repeated syntax mistake at the end of each function parameter to then.
The syntax is:
somePromise.then(function() {
// success
}).then(function() { // your code says '}.' instead of '}).'
// success
});
There's an optional error function callback as second param:
somePromise.then(function() {
// success
}).then(function() {
// success
}, function(error) {
// error
});
You are missing the close parentheses for each promise callback. Your code should be this :
var url = file.url();
Parse.Cloud.httpRequest({ url: url }).then(function(response) {
// Create an Image from the data.
var image = new Image();
return image.setData(response.buffer);
}).then(function(image) {
// Scale the image to a certain size.
return image.scale({ width: 64, height: 64 });
}).then(function(image) {
// Get the bytes of the new image.
return image.data();
}).then(function(buffer) {
// Save the bytes to a new file.
var file = new Parse.File("image.jpg", { base64: data.toString("base64"); });
return file.save();
});

order of operations parse cloud

I was wondering what the order of operations are in the parse cloud. I currently am running into trouble trying to do multiple things at once inside my job on the cloud. I am currently trying to make an HTTP request for each user in my user table (there are 2) and then get the webpage or httprequest.text from the webpage. My code is as followed
Parse.Cloud.job("WeatherUpdates2", function(request, status) {
var query = new Parse.Query(Parse.User);
query.exists("City");
query.each(
function(result){
var object = result;
console.log(object.id);
var city = object.get("City");
city = city.replace(" ", "");
city = city.replace(" ", "");
// get the country code.
var countryCode = object.get("CountryCode");
var woeidUrl = "http://where.yahooapis.com/v1/places.q(" + city + "," + countryCode + ")?appid=(appid)";
console.log(woeidUrl);
var woeID = "An error occured retrieving your WOEID.";
Parse.Cloud.run('httpRequest', { url: woeidUrl }, {
success: function(WOEID) {
console.log("returned from http request.");
},
error: function(error) {
console.log("Error occurred while making request for WOEID " + error.message);
status.error(error.message);
}
});
},
{
success: function() {
// results is an array of Parse.Object.
console.log('#Query');
status.success("Updated Records!!");
},
error: function(error) {
// error is an instance of Parse.Error.
console.log('#error');
response.error("Failed to save vote. Error=" + error.message);
}
});
});
Where the job httpRequest is:
Parse.Cloud.define("httpRequest", function(request, response) {
var webpage = "Something went wrong.";
Parse.Cloud.httpRequest({
url: request.params.url,
success: function (httpResponse) {
webpage = httpResponse.text;
webpage = webpage.toString();
response.success(webpage);
},
error: function (error)
{
console.log("Error in http request " + error.message);
response.error(error.message);
}
});
});
now I would expect to be printed would be the, object id of first user, their url, the job running, the message"returned from http request." then repeated another time for the second user and finally the job finishing with the message "Updated Records". but instead I get:
I2014-07-22T15:15:16.013Z] A5hod7qKE3
I2014-07-22T15:15:16.045Z] http:/where.yahooapis.com/v1/places.q(Draper,US)?appid=(appid)
I2014-07-22T15:15:16.053Z] GmuqxpTUpM
I2014-07-22T15:15:16.066Z] http:/where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid=(appid)
I2014-07-22T15:15:16.082Z] #Query
I2014-07-22T15:15:16.131Z] v385: Ran cloud function httpRequest with:
Input: {"url":"http://where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid=(appid)"}
Result:
2487610TownSalt Lake CityUnited StatesUtahSalt LakeSalt Lake City40.777561-111.93071740.699890-112.10125740.852951-111.739479511America/Denver
I2014-07-22T15:15:16.141Z] v385: Ran cloud function httpRequest with:
Input: {"url":"'http://where.yahooapis.com/v1/places.q(Draper,US)?appid=(appid)'"}
Result:
http://where.yahooapis.com/v1/schema.rng'" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:start="0" yahoo:count="1" yahoo:total="11">2393601TownDraperUnited StatesUtahDraper8402040.524139-111.86627240.442921-111.92212740.544361-111.78304349America/Denver
I removed 1 / from both the printing urls so I could posts this because I don't have high enough rep to post more than 2 links. I also have put in my appid into the (appid) so the url does return to me the according woeid from yahoo.com. The problem here being I can't actually get into the success function of the http request job. Any help is greatly appreciated!
EDIT:
I was trying to figure out how to run a job in a for loop but couldn't get it to work. I tried to make a promise and do what Fosco said below, but have had no luck. Here is my code.
for(var i = 0; i < woeIDs.length; i++)
{
console.log("hello");
var weatherURL = "http://weather.yahooapis.com/forecastrss?w=" + woeIDs[i] + "&u=f";
var promise = Parse.Cloud.run('httpRequest', { url: weatherURL }, {
success: function(WOEID) {
console.log("got here");
},
error: function(error) {
console.log("Error occurred while making request for WOEID " + error.message);
status.error(error.message);
}
});
Parse.Promise.when([promise]).then(function() { status.success(); });
}
if I run this code I get a hello twice then the two job calls then the "got here" message once. I have tried adding a return statement to it and with no luck also. Thanks again for all the help!!!
The issue here is that inside the each callback, you need to return the promise from your cloud function call if you want to ensure the tasks complete, and have it wait before going to the next object.
Simplified and using promises:
query.each(function(object) {
...
return Parse.Cloud.run(...);
}).then(function() {
// success
}, function(err) {
// error
});
For looping over a promise-returning function like Parse.Cloud.run:
var promises = [];
for (i = 0; i < 5; i++) {
promises.push(Parse.Cloud.run('...', {}));
}
Parse.Promise.when(promises).then(function() {
// all done
}, function(err) {
// error
});

Categories

Resources