I'm attempting to use Cloud Code on Parse to delete pre-existing rows as new rows are added.
This is the current code I've put together:
var queryGoals = new Parse.Query(Parse.Object.extend('Goal'));
queryGoals.include('user');
queryGoals.equalTo('user', request.user);
queryGoals.find({
success: function(results) {
console.warn('Query Returned: ' + results.length);
Parse.Object.destroyAll(results);
},
error: function(user, error) {
console.warn('Error ' + error.code + ': ' + error.message);
}
});
The console never shows the number of records returned (I assume because none are), nor any errors. The same code (with Parse.User.current() for the user filter, of course) returns the expected number of rows when run client-side.
Do queries operate differently when executed by Cloud Code, or have I overlooked something?
I filed a bug report with Parse and finally ended up finding out that the function was ending before the success function was executed since it was running asynchronously. I added in the proper promise structure to get it working as expected (Additional Details):
queryGoals.find(
{
useMasterKey: true,
success: function(results)
{
Parse.Object.destroyAll(results);
},
error: function(user, error)
{
console.warn('Error ' + error.code + ': ' + error.message);
}
}).then(
function(object)
{
// Do Post-Query Stuff Here
response.success();
},
function(error)
{
console.warn('Error ' + error.code + ': ' + error.message);
}
);
Related
When I am posting a picture from my electron app to blob storage, sometimes it works, and other times I get this error on my terminal:
When I was first working on this app, this problem never showed up, until a week ago. It occurred without making any changes to this part of the app. Any idea on what could cause it.
The electron app goes white, and the dev tools are disconnected.
Here is the code:
var azure = require('azure-storage');
var blobSvc = azure.createBlobService('*connection keys inside here*');
function createBlob() {
blobSvc.createContainerIfNotExists('photos', {publicAccessLevel : 'blob'}, function(error, result, response){
if(!error){
console.log(response);
}
});
console.log("creating image for student#: " + stud_id);
blobSvc.createBlockBlobFromStream('photos', stud_id + '.jpg', toStream(imgData), imgData.size, function(error, result, response){
if(!error){
console.log("file upload: \n" + JSON.stringify(result) + " \n" + JSON.stringify(response));
createPerson();
}
else if (error) {
console.log("error: " + JSON.stringify(error));
}
});
}
In your code, you actually call the createBlockBlobFromStream immediately, probably without container having created. This may cause the problem.
So, you would need to put them within the callback of the createContainerIfNotExists function:
blobSvc.createContainerIfNotExists('photos', {publicAccessLevel : 'blob'}, function(error, result, response) {
if(!error) {
console.log(response);
console.log("creating image for student#: " + stud_id);
blobSvc.createBlockBlobFromStream('photos', stud_id + '.jpg', toStream(imgData), imgData.size, function(error, result, response) {
if(!error) {
console.log("file upload: \n" + JSON.stringify(result) + " \n" + JSON.stringify(response));
createPerson();
} else {
console.log("error: " + JSON.stringify(error));
}
});
}
});
I have some problem with parse cloud js.
After I save ParseMessage object, I want to check 3 pointer (product, sender and recipient) in other class (MessageTracker). With the base 3 query there is no problem, but when I call insideQuery.notEqualTo("sender", result.get("owner")); I got an error:
102 : pointer field sender needs a pointer value
This "owner" also a pointer to user class, like the others (sender and recipient), but this time parse cloud send an error.
(On console I see, that the object ids are valid values, so I have the right objects)
Parse.Cloud.afterSave("ParseMessage", function(request) {
var message = request.object;
var product = message.get("product");
var sender = message.get("sender");
var recipient = message.get("recipient");
var query = new Parse.Query("Product");
query.get(product.id, {
success: function(result) {
console.error("prod owner: " + result.get("owner").id + " sender: " + sender.id + " reciever: " + recipient.id);
var insideQuery = new Parse.Query("MessageTracker");
insideQuery.equalTo("product", product);
insideQuery.equalTo("sender", sender);
insideQuery.equalTo("recipient", recipient);
insideQuery.notEqualTo("sender", result.get("owner"));
insideQuery.find({
success: function(results) {
console.error("count: " + results.length);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
},
error: function(object, error) {
}
});
});
Any good advice?
UPDATE
I maybe found the problem. Parse do not let us check equalTo and notEqualTo for the same pointer in the query. Is there any other way to do that?
Try building out the pointer:
var ownerPointer = {
__type: 'Pointer',
className: <className>,
objectId: result.get("owner").id
};
insideQuery.notEqualTo("sender", ownerPointer);
So I'm trying to pull the name of the creator, just for testing purposes, from the session object. The creator object is a pointer but it keeps coming up as undefined however? Any clue why?
function joinLobby(){
var ses = Parse.Object.extend("Session");
var query = new Parse.Query(ses);
query.equalTo("Name", document.getElementById("lobby").value);
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length );
// Do something with the returned Parse.Object values
session = results[0];
addPlayer(session,player);
console.log("session joined: "+session.get("Name"));
console.log("The creator is: "+session.get("Creator").get("Name")); // coming up as undefined
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
This is because the query does not load the object pointed by the pointer. To do that you need to add this extra line and then run your query.
query.include("Creator");
I am trying to update other user's information in Parse which is stored in User class.
Initially I tried following code:
1.
var user = Parse.User;
var query = new Parse.Query(user);
query.equalTo("name", userName); //to find specific user
query.first({
success: function(results) {
//update that user's details
results.set("name", newName);
results.save();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
I got following error:
{code: 206, error: "Parse::UserCannotBeAlteredWithoutSessionError"}
2. Then I used Parse.Cloud.useMasterKey(), as I figured out that we can not edit other user's details. But it didn't work either. And master key overrides all security, so it was not a good idea to use this code.
3. So I tried following code:
var user = Parse.User;
var query = new Parse.Query(user);
query.equalTo("objectId", userName);
query.first({
success: function(results) {
results.set("name", newName);
results.save(null, { useMasterKey:true}).then(function(user) {console.log("updated!!!");}, function(error) {console.log(error); });
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
But I am getting '401 Unauthorized' error.
Does anyone know how to update other user's information in Parse? Any help would be greatly appreciated.
You should call fetch function after updated current user object. You can follow below sample:
var currentUser = Parse.User.current();
currentUser.set('name', 'New Name');
currentUser.save(null, {
success: function(user) {
currentUser.fetch();
},
error: function(user, error) {
alert('Failed to update object, with error code: ' + error.message);
}
});
Below is my JS code that is supposed to delete a pointer reference to a 'Game' object which is stored in an array under the key 'games' in the 'team' object. Fetching works and all the 'success' logs below get called meaning that the .remove and .save functions get called, but for some reason, this does not do anything and the pointers still exist in the arrays after the execution of this code.
What am I doing wrong?
var queryTeam = request.object.get("awayTeamID");
queryTeam.fetch({
success: function(awayTeam){
console.log("Success in Fetching awayTeam Object. Destroying Now.");
awayTeam.remove(awayTeam.get("games"), request.object);
awayTeam.save();
var queryTeam = request.object.get("homeTeamID");
queryTeam.fetch({
success: function(homeTeam){
console.log("Success in Fetching homeTeam Object. Destroying Now.");
homeTeam.remove(homeTeam.get("games"), request.object);
homeTeam.save();
},
error: function(homeTeam, error){
console.error("Error removing game from homeTeam array! " + error.code + ": " + error.message);
}
});
},
error: function(myObject, error){
console.error("Error removing game from awayTeam array! " + error.code + ": " + error.message);
}
});
Your approach to removing is incorrect, you don't need to fetch the array to remove it, the remove request should be made on the object:
awayTeam.remove("games", request.object);