Forum Discussion

RyanStanford1's avatar
RyanStanford1
Qrew Captain
7 years ago

API_AddRecord from JavaScript Array creating duplicates

Hello,

I am using Javascript to pull out a row from one table, remove duplicates, and then I want to upload those results into another table, one record for each value.

Distinct is the array, and it has the proper values. The issue I'm having is that it appears to be that the for loop is going through the values twice, as when I'm expecting 2 rows, I'm getting 4.

        Distinct.forEach(function(entry){
             for(index = 0, len = Distinct.length; index < len; ++index){
                  var xhttp = new XMLHttpRequest();
                  xhttp.open('POST', "[REALM]/db/[DBID]?a=API_AddRecord&[TOKEN]&_fid_6="+Distinct[index],true);
                 xhttp.send();
             }
        })

I know that I'm probably overlooking something simple, like somehow closing the request, or trying to open it more than once...
  • Try this:

    var dbid = "your application dbid";
    var dbidTable = "your table dbid"
    var apptoken = "your application token";
    $.ajaxSetup({data: {apptoken: apptoken}});

    var distinct = ["foo", "bar", 'baz"];
    var csv = distinct.join("\n");

    $.post(dbidTable,{
      act: "API_ImportFromCSV",
      records_csv: csv,
      clist: "6"
    });
  • Dude... I was thinking the API_ImportFromCSV was going to be a lot more difficult than that... I'll check this out now.
  • It's erroring out stating that "Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."

    I believe that this is actually because I'm currently working from a local .html file on my desktop prior to moving this into the QB environment.
    • RyanStanford1's avatar
      RyanStanford1
      Qrew Captain
      Still not liking it. When I paste it into Codepen.io to test, it's now showing a 404 error when trying to perform the upload csv api
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      Paste the code into the console of your browser while on your QuickBase page. Codepen is on another domain so the relative URL dbidTable in the $.post() method will result in a 404 error.