Reputation: 401
I am having trouble with Parse in Javascript. I am just trying to loop through my list of objects that I have in an array. If the imageURL matches one in Parse, then do nothing. If their is no imageURL from the array, in Parse, then add the row to Parse.
I think my issue is synchronicity. Its is currently saving the same row (same name, imageURL, and fbURL) many times to Parse. Basically saving one row, and its duplicated 70 times.
for (var i = 0; i < listobjects.length; i++) {
objName = listobjects[i]["name"]
objImageURL = listobjects[i]["imageURL"]
objfbURL = listobjects[i]["fbURL"]
console.log("NAME: " + listobjects[i]["name"])
// Check if record is in Parse
var query = new Parse.Query(Recipe);
query.equalTo("imageURL", listobjects[i]["imageURL"]);
query.find({
success: function(results) {
if (results.length > 0) {
// Do nothing. We have a result. Or maybe check if videoURL is present
console.log("Object is already in DB.")
} else {
// Save the new object.
var recipeObject = new Recipe();
recipeObject.save({name: objName, imageURL:objImageURL, fbURL: objfbURL}, {
success: function(object) {
console.log("Object Saved!")
},
error: function(model, error) {
console.log("Error saving object!")
}
});
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
Upvotes: 0
Views: 118
Reputation: 1415
Change
for (var i = 0; i < listobjects.length; i++) {
objName = listobjects[i]["name"]
// ...
}
to
listobjects.forEach(function(obj) {
// access obj.name, obj.imageURL
// ...
});
to create a scope and the right object will be used within the find callback.
Upvotes: 1