API_AddRecord from JavaScript Array creating duplicates

  • 0
  • 1
  • Question
  • Updated 3 months ago
  • Answered
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...
Photo of Ryan Stanford

Ryan Stanford

  • 542 Points 500 badge 2x thumb

Posted 3 months ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 28,144 Points 20k badge 2x thumb
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"
});
Photo of Ryan Stanford

Ryan Stanford

  • 542 Points 500 badge 2x thumb
Dude... I was thinking the API_ImportFromCSV was going to be a lot more difficult than that... I'll check this out now.
Photo of Ryan Stanford

Ryan Stanford

  • 542 Points 500 badge 2x thumb
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.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 28,004 Points 20k badge 2x thumb
Paste your code into the console to test it.
Photo of Ryan Stanford

Ryan Stanford

  • 542 Points 500 badge 2x thumb
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
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 28,004 Points 20k badge 2x thumb
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.
(Edited)
Photo of Ryan Stanford

Ryan Stanford

  • 542 Points 500 badge 2x thumb
That did it... for some reason I got hungup on the dbid vs dbidTable as well.

Now to just turn this into a codepage and still have it work.
(Edited)