Change URLs into files (Parse.com using Javascript CloudCode) - javascript

I need to batch change a number of image links (URL's links that exist within a class in) to image files (that Parse.com hosts).
Cloud code is (apparently) how to do it.
I've followed the documentation here but haven't had any success.
What I wanted to do is:
Take URL link from "COLUMN_1"
Make it a file
Upload file to "COLUMN_1" (overwrite existing URL). If this is dangerous- can upload it to a new column ("COLUMN_2").
Repeat for next row
This code did not work (this is my first time with JS):
imgFile.save().then(function () {
object.set("COLUMN_1", imgFile);
return object.save();
}).then(function (CLASSNAME) {
response.success("saved object");
}, function (error) {
response.error("failed to save object");
});
Can anyone recommend how to do this?

OK- this successfully works for anyone else trying.
Parse.Cloud.job("convertFiles", function(request, status) { //Cuts the rundata out of poor runs
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
// Tell the JS cloud code to keep a log of where it's upto. Manually create one row (in class "debugclass") to get an object Id
Parse.Cloud.useMasterKey();
var Debug = Parse.Object.extend("debugclass");
var queryForDebugObj = new Parse.Query(Debug);
queryForDebugObj.equalTo("objectId", "KbwwDV2S57");
// Query for all users
// var queryForSublist = new Parse.Query(Parse.Object.extend("gentest"));
queryForDebugObj.find({
success: function(results) {
var debugObj = results[0];
var processCallback = function(res) {
var entry = res[0];
var debugObj = results[0];
debugObj.set("LastObject", entry.id);
debugObj.save();
Parse.Cloud.httpRequest({
url: entry.get("smallImage2"),
method: "GET",
success: function(httpImgFile)
{
console.log("httpImgFile: " + String(httpImgFile.buffer));
var imgFile = new Parse.File("picture.jpg", {base64: httpImgFile.buffer.toString('base64')});
imgFile.save().then(function () {
console.log("2");
entry.set("smallImage1", imgFile);
entry.save(null, {
success: function(unused) {
debugObj.increment("itemDone");
sleep(20);
res.shift();
if (res.length === 0) {
process(entry.id);
return;
}
else {
processCallback(res);
}
},
error: function(unused, error) {
response.error("failed to save entry");
}
});
});
},
error: function(httpResponse)
{
console.log("unsuccessful http request");
response.error(responseString);
}
});
};
var process = function(skip) {{
var queryForSublist = new Parse.Query("genpants");
if (skip) {
queryForSublist.greaterThan("objectId", skip);
console.error("last object retrieved:" + skip);
}
queryForSublist.ascending("objectId");
queryForSublist.find().then(function querySuccess(res) {
processCallback(res);
}, function queryFailed(reason) {
status.error("query unsuccessful, length of result " + result.length + ", error:" + error.code + " " + error.message);
});
}};
process(debugObj.get("LastObject"));
},
error: function(error) {
status.error("xxx Uh oh, something went wrong 2:" + error + " " + error.message);
}
});
});

Related

Variable scope; var undefined or same var loop

Meteor.methods({
'list.messages' () {
getTwilioMessages(function(err, data) {
if (err) {
console.warn("There was an error getting data from twilio", err);
return
}
data.messages.forEach(function(message) {
if (SMS.find({
sid: message.sid
}).count() > 0) {
return;
}
var image;
if (message.numMedia > 0) {
var images = [];
Meteor.wrapAsync(client.messages(message.sid).media.list(function(err, data) {
console.log(data)
if (err) throw err;
Meteor.wrapAsync(data.media_list.forEach(function(media, index) {
mediaImage = (function() {
let mediaImg = 'http://api.twilio.com/2010-04-01/Accounts/' + media.account_sid + '/Messages/' + media.parent_sid + '/Media/' + media.sid;
return mediaImg
});
}));
images.push(mediaImage());
console.log("Just One:" + mediaImage())
console.log("IMAGESSSS " + mediaImage())
}));
message.image = mediaImage();
console.log("test" + image)
} else {
message.image = null
}
if (message.from === Meteor.settings.TWILIO.FROM) {
message.type = "outgoing";
} else {
message.type = "incoming";
}
SMS.insert(message)
});
});
getTwilioMessages();
Meteor.setInterval(getTwilioMessages, 60000);
// client.calls.get(function(err, response) {
// response.calls.forEach(function(call) {
// console.log('Received call from: ' + call.from);
// console.log('Call duration (in seconds): ' + call.duration);
// });
// });
},
I expect to see different image URL's in the data.media_list.forEach. But, it returns the same image. In the console I get the desired results.
I want these results:
message.image = 'http://api.twilio.com/2010-04-01/Accounts/3039030393/Messages/303030/media/3kddkdd'
message.image = 'http://api.twilio.com/2010-04-01/Accounts/3039sdfa393/Messages/303030/media/3asdfdfsa' inserted into the correct message.
instead I get the same URl over and over resulting in the same image
for all my messages. I guess my scope is incorrect, but I have
attempted to follow: 2ality guide on scoping and Explaining
JavaScript Scope And Closures with no luck.

using variable to change Parse.com query

I am using the following Parse.com Javascript query and need to switch the type of query based on a variable.
function searchParseExercises (queryParam, ActiveFilters, searchParam) {
var exercise = [];
var TagSearch = Parse.Object.extend("Exercises");
var query = new Parse.Query(TagSearch);
query.containsAll("tags", ActiveFilters);
query.limit(20);
query.find({
success: function(results) {
var exerciseData = {};
for (var i = 0; i < results.length; i++) {
var object = results[i];
var exerciseData = {
exerciseName : object.get('exerciseName'),
exerciseDescription : object.get('exerciseDescription'),
images : object.get('images'),
}
exercise.push(exerciseData);
}
$scope.allExercises = exercise;
},
error: function(error) {
$ionicPopup.alert({
title: "Error: " + error.code + " " + error.message
});
}
});
}
To clarify the requirement I have both a text search and filter search in my template. If there is a text search then it should perform:
query.contains("exerciseName", searchParam);
If there are ActiveFilters then it should perform this:
query.containsAll("tags", ActiveFilters);
However if both variables are present (searchParam and ActiveFilters)
then it should perform a Compound Query.
I have no idea how I can wire all this up cleanly.
What I understood from your question:
if (ActiveFilters && searchParam) {
Parse.Query.or(ActiveFilters, searchParam).find({
success: function(results) {
// results contains a list of players that either have won a lot of games or won only a few games.
},
error: function(error) {
// There was an error.
}
}
else if (ActiveFilters) {
query.containsAll("tags", ActiveFilters);
}
else if (searchParam) {
query.contains("exerciseName", searchParam);
}

Cloud code skipping section of code

I never learnt javascript so please bear with me.
I have a cloud function that does all but the indicated section (>>):
Parse.Cloud.define("acceptRequest", function(request, response) {
var user = request.user;
var requestUser;
var requestObject;
var requestId = request.params.objectId;
var query = new Parse.Query(GameRequests);
query.get(requestId,{
//Get GameRequest Object - requestObject
success: function (object) {
var requestObject = object;
var sender = requestObject.get("from");
var senderId = sender[0];
var query = new Parse.Query(Parse.User);
query.get(senderId, {
//From GameRequest Object data, find sender of request "requestUser"
success: function (userObject) {
var requestUser = userObject;
var requestUserList = requestUser.get("SENTrequests")
var requestIndex = requestUserList.indexOf(requestId);
if (requestIndex > -1) {
requestUserList.splice(requestIndex, 1);
requestUser.set("SENTrequests",requestUserList);
if (requestUserList.length = 1) {
user.unset("SENTrequests");
}
}
var userList = user.get("RCDrequests");
var userIndex = userList.indexOf(requestId);
if (userIndex > -1) {
userList.splice(userIndex, 1);
user.set("RCDrequests",userList);
if (userList.length = 1) {
user.unset("RCDrequests");
}
}
requestObject.destroy({
success: function(requestObject) {
requestUser.add("partners",user.id);
user.add("partners",requestUser.id);
var firstmsg = new GameMessage();
var secondmsg = new GameMessage();
firstmsg.set("sender", user.id);
firstmsg.set("receiver", requestUser.id);
firstmsg.set("sent", 0);
firstmsg.set("received", 0);
firstmsg.set("receiverName", requestUser.getUsername());
secondmsg.set("sender", requestUser.id);
secondmsg.set("receiver", user.id);
secondmsg.set("sent", 0);
secondmsg.set("received", 0);
secondmsg.set("receiverName", user.getUsername());
Parse.Object.saveAll([requestUser, user, firstmsg, secondmsg], { useMasterKey: true },{
>> success: function() {
>> console.log("Saving messages again");
>> firstmsg.set("otherside", secondmsg.id);
>> secondmsg.set("otherside", firstmsg.id);
>>
>> Parse.Object.saveAll([firstmsg, secondmsg]);
},
error: function(error) {
}
});
response.success("Successful");
},
error: function(requestObject, error){
}
});
},
error: function (userObject,error) {
}
});
},
error: function (object,error) {
response.error("Error");
}
});
});
It is supposed to save the two messages' objectIds so each has a reference to the other.
What is causing this problem and how can I fix it?
Thank you
I think your sending your response.success("Successful") before the save has been completed. Move your response to the success handler of the save.
You should take a look at promises section. You will not need the deep nested functions you have currently.

Apigee Usergrid: Mass delete option missing

I am using usergrid to store data for a customer project. It's got two collections carShowrooms and cars. So far I am good. But I have a scenario where I have refresh the masterdata of the collection cars. Everytime I do this, I have to delete all the existing data in cars and replace it with incoming cars data from the master inventory system.
Now, with the docu in https://www.npmjs.org/package/usergrid, I see that I can only destroy one car at a time.
car.destroy(function(err){
if (err){
//error - car not deleted
//winston log - tbd
} else {
//success - car deleted
}
});
This is ok for smaller showrooms, but bigger multibrand showrooms have variety of cars - sometimes even upto 50 different varieties (8 car brands * approx. 8 different options).
Is there a mass delete option? can someone please point me to a docu if I am missing something here.
P.S. I am new to usergrid, if this is a repeated question, please mark so and point me to the right url
If you're so inclined, I've written a Node.js bulk deleter that runs delete requests in parallel. It takes approximately 3 minutes to delete 1000 entities.
Here's an always up-to-date gist, and a copy for SO:
// Installation
// 1. Install Node.js http://nodejs.org/download/
// 2. In Terminal, cd (navigate) to the directory where you saved this file
// 3. Run 'npm install request async'
// 4. Edit the script config below with your token, org, app, and collection name.
// 5. To run the script, at the Terminal prompt, run 'node api_baas_deleter.js'
// Config
var access_token = "{token}";
var as_basepath = "http://api.usergrid.com/{org}/{app}/"; // You need the trailing slash!
var collection = "{collection_name}";
// End Config
var request = require('request');
var async = require('async');
var authstring = "access_token=" + access_token;
var total = 0;
var startTime = Date.now();
function deleteRecords(callback) {
request.get({
url: as_basepath + collection + "?" + authstring,
json: true
}, function(e, r, body) {
if (body.count === undefined) {
var err = "Error: invalid endpoint. Check your basepath and collection name.";
console.log(err);
if (typeof(callback) === 'function') {
callback(err)
}
} else {
// console.log("Found " + body.count + " entities");
if (body.count > 0) {
var deletes = [];
for (var i = 0; i < body.count; i++) {
deletes.push({
url: as_basepath + collection + "/" + body.entities[i].uuid + "?" + authstring,
json: true
});
console.log("Deleting " + body.entities[i].uuid)
}
async.each(deletes, function(options, callback) {
request.del(options, function(e, r, body) {
if (r.statusCode === 200) {
total++;
}
callback(e);
});
}, function(err) {
setTimeout(function() {
deleteRecords(collection, function(e) {
callback(e);
});
}, 600); // Mandatory, since it seems to not retrieve entities if you make a request in < 600ms
});
} else {
var timeInMinutes = minutesFromMs(Date.now() - startTime);
console.log("Deleted " + total + " entities in " + timeInMinutes + " minute" + ((timeInMinutes > 1 || timeInMinutes < 1) ? "s" : ""));
if (typeof(callback) === 'function') {
callback()
}
}
}
});
}
function minutesFromMs(time) {
return Math.round(((time % 86400000) % 3600000) / 60000).toString();
}
deleteRecords();
There currently isn't a mass delete function in the Usergrid Node SDK, but you can create one. This is how I added a monkey-patched delete-by-query function into the Node SDK:
Usergrid.client.prototype.delete = function(opts, callback) {
if (_.isFunction(opts)) { callback = opts; opts = undefined; }
if (!opts.qs.q) { opts.qs.q = '*'; }
var options = {
method: 'DELETE',
endpoint: opts.type,
qs: opts.qs
};
var self = this;
this.request(options, function (err, data) {
if (err && self.logging) {
console.log('entities could not be deleted');
}
if (typeof(callback) === 'function') {
callback(err, data);
}
});
};
Hope that helps!
Scott

Parse.com Cloud Code - saveAll Request timed out

I'm trying to save a collection of objects from a Cloud Code function.
After 30-40 objects I get a time out error. My code looks like this:
Parse.Cloud.define("saveInBackground", function (request, response) {
console.log("saveInBackground begin");
var objectsToSave = [];
for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) {
objectsToSave.push(new LikedObject(request.params.collectionToSave[i]));
};
Parse.Object.saveAll(objectsToSave, {
success: function(list) {
// All the objects were saved.
if (response) {
response.success(list);
};
console.log("saveInBackground success");
},
error: function(model, error) {
// An error occurred while saving one of the objects.
if (response) {
response.error(error);
};
console.log("saveInBackground error: " + error.message);
}
});
console.log("saveInBackground end");
});
Can I do something else in order to save a bunch of objects?
Depending on the size of your objects you can try to save them in batches of 20-30. This is required since saveAll() is trying to upload all the objects you give it to the server. Here's sample code of how you might do it:
var result = true;
for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) {
objectsToSave.push(new LikedObject(request.params.collectionToSave[i]));
if (i % 10 == 0) {
result = saveObjects(objectsToSave);
objectsToSave.length = 0;
}
};
if (result == true) {
console.log("saveInBackground success");
}
function saveObjects(objects) {
Parse.Object.saveAll(...);
...
}

Categories

Resources