0

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);
        }
      });

    }
1
  • You've not given enough here to reproduce the issue. Commented Mar 7, 2016 at 22:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.