1

I am trying to check if the data already exists in the JSON Array Object that is returned on 'GET'. IF the data already exists, I want it to ignore the data. If it does not exist I want it to "POST." Please help. It seems to be skipping the "POST" call.

  function POST(section){
     $.ajax({
        type: 'POST',
        dataType: 'json',
     async : false,
        data: JSON.stringify(section),
        url: '/api/v2/help_center/en-us/categories/200384077/sections.json',
        contentType: "application/json; charset=utf-8",

        headers: {
            "Authorization": "Basic " + btoa(username + ":" + password)
        },// end headers
        success: function (newSection) {
            console.log("Congrats! We have inserted a new section", newSection);

        },
        error: function (xhr, errorText) {
            console.log('Error ' + xhr.responseText);
        }
    })// end AJAX POST
    }

function addNewSection(name, description) {

    var section = { "section": { "position": 2, "name": name, "description": description, "content.translations.name": [{ "locale": "en-us" }] } };

    $.ajax({
        type: 'GET',
        url: '/api/v2/help_center/categories/200384077/sections.json',
        dataType: 'json',
        async : false,
        headers: {
            "Authorization": "Basic" + btoa(username + ":" + password)
        },
        success: function (mySections) {

            var naming = mySections.sections;
            $.each(naming, function (i, result) {
              if( $.inArray(name, result) == -1)
              {
                console.log("This already exists", name);
              } 

                if( !$.inArray(name, result) == -1) {
              POST(section);
              }

            });

        } // end success function
    })// end AJAX GET


}

2 Answers 2

1

Remove the ! from this:

if( !$.inArray(name, result) == -1)

So you have:

if ($.inArray(name, result) == -1)

What you wrote is basically a double negative. $.inArray returns -1 when it doesn't find anything. You would never use ! with $.inArray because it can never return false.

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

7 Comments

I used if( !$.inArray(name, result) == -1) if the data doesn't exist. then "POST." Is there a better way to write it? because If i use if( $.inArray(name, result) == -1) again then i will insert duplicate data.
if( $.inArray(name, result) == -1) already literally means "If the name is NOT in the result array". Isn't that when you want to post?
If you want it the other way, where you post only if the name IS in the array, you'd do if( $.inArray(name, result) >= 0). Either way, you would never write !$.inArray as the function has no way of ever returning "false." It returns -1 if it doesn't find anything, or an index (0 or higher) if it does find something.
When using >=0 it always says the data exists in the array even when it doesn't. Any other ideas?
You're going to have to post your code where that's happening, because there's something else going on. If $.inArray comes back with 0 or greater, it found a match...there's no way for $.inArray to get it wrong.
|
0

Looks like this worked just perfectly!!

var match = false;
$.each(naming, function(i,v){
  if(v.name == name){
         match = true;
        return false;
    }
 });
 if(!match){
  POST(section);
}

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.