0

I am working on the below function in Javascript to pull data from two separate lists on SharePoint and pass the resultant data to another function. I've spent the past few hours trying to debug this function with the following error:

jQuery.Deferred exception: Duplicate parameter name not allowed in this context SyntaxError: Duplicate parameter name not allowed in this context at Array.

and am told the error is occurring at this line:

  successFunction(RecordResponse, RecordAmountResponse)

Strangely, it do passed through both of the .done functions and output their respective check flags, yet I know I cannot change the parameters in the .then(function() { } ) otherwise my successFunction won't receive the response data it needs.

Here is the function that I am calling:

function getReportData(strRecordQuery, strRecordAmountQuery, successFunction, failFunction) {

    // Use promise methods to run getListQuery from both tblRecord and tblRecordData

    var RecordDataInfo = $.Deferred();
    var RecordAmountDataInfo = $.Deferred();

    var dfdRecordDataFiles;
    var dfdRecordAmountDataFiles;

    dfdRecordDataFiles = getListQuery('lstRecord', strRecordQuery, true);

    dfdRecordAmountDataFiles = getListQuery('lstRecordAmount', strRecordAmountQuery, true);

    dfdRecordDataFiles.done(function(RecordDataResponse) {
        console.log("dfdRecordDataFiles.done");
        RecordDataInfo.resolve(RecordDataResponse);
    });

    dfdRecordAmountDataFiles.done(function(RecordAmountDataResponse) {
        console.log("dfdRecordAmountDataFiles.done");
        RecordAmountDataInfo.resolve(RecordAmountDataResponse);
    });

    dfdRecordDataFiles.fail(function(error) {
        console.log("dfdRecordDataFiles.fail");
        console.log("ResponseText: " + error.responseText);
        console.log(JSON.stringify(error));
        RecordDataInfo.fail(error);
    });

    dfdRecordAmountDataFiles.fail(function(lerror) {
        console.log("dfdRecordAmountDataFiles.fail");
        console.log("ResponseText: " + lerror.responseText);
        console.log(JSON.stringify(lerror));
        RecordAmountDataInfo.fail(lerror);
    });

    $.when(RecordDataInfo, RecordAmountDataInfo)
        .then(function(RecordResponse, RecordAmountResponse) {
          successFunction(RecordResponse, RecordAmountResponse)
        }, function(error, lerror) {
          failFunction(error, lerror);
        });
};

Can I get a second set of eyeballs to help me, as mine are getting tired of staring at this and not getting it.

Edit

I modified my code as per suggestion but am getting similar error:

function getListQuery(listName, queryString) {

    // REST Call
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?" + queryString,
        type: "GET",
        headers: { ACCEPT: "application/json;odata=verbose" }
    });
};

function getReportData(strRecordQuery, strRecordAmountQuery, strRecordPersonnelQuery) {

    return $.when(getListQuery('lstRecord', strRecordQuery, true), getListQuery('lstRecordAmount', strRecordAmountQuery, true), getListQuery('lstRecordPersonnel', strRecordPersonnelQuery, true))
};

function setReportQueries(Yr0, Yr1, Yr2, Yr3, Yr4) {

    // Specific to the report
    strRecordQuery = "$select=ID,RecordName";
    strRecordAmountQuery = "$select=ID,Parameter1,Year,Amount&$filter=(Year eq '" + Yr0 + "') or (Year eq '" + Yr1 + 
                        "') or (Year eq '" + Yr2 + "') or (Year eq '" + Yr3 + "') or (Year eq '" + Yr4 + "')"; 
    strRecordPersonnelQuery = "$select=ID, Parameter1&$filter=(Year eq '" + Yr0 + "') or (Year eq '" + Yr1 + 
                        "') or (Year eq '" + Yr2 + "') or (Year eq '" + Yr3 + "') or (Year eq '" + Yr4 + "')";

    getReportData(strRecordQuery, strRecordAmountQuery, strRecordPersonnelQuery).then(loadReportTables);

};

function loadReportTables(RecordData, RecordAmountData, RecordPersonnelData) {

console.log("Enter loadReportTables()");

/* Do Stuff */

}

The error I am getting is:

jQuery.Deferred exception: Duplicate parameter name not allowed in this context SyntaxError: Duplicate parameter name not allowed in this context at l (https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js:2:29375) at c (https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js:2:29677) undefined

BWieland86
  • 35
  • 2
  • 8
  • Avoid the [deferred antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! And just return a promise instead of taking `successFunction` and `failFunction` callbacks (that are not even optional) – Bergi Dec 09 '18 at 22:41
  • The reasoning for it in this instance is I need the successFunction to be run only when both of the getListQuery AJAX calls have completed and returned the data from my lists, as my successFunction then takes the data and processes it from there. I read through the cited article and the resolution to the "deferred anti-pattern" you mentioned was to "check the library API." Which one? Is there some example code of what should be done instead? – BWieland86 Dec 09 '18 at 23:40
  • Would it be re-written more simply as: Promise.all([getListQuery('lstRecord', strRecordQuery, true), getListQuery('lstRecordAmount', strRecordAmountQuery, true]) .then(([RecordResponse, RecordAmountResponse]) => { successFunction(RecordResponse, RecordAmountResponse) }); – BWieland86 Dec 09 '18 at 23:57
  • Yes, you can write it like that, or with `$.when` instead of `Promise.all`. However I recommend to drop the `.then(…);` in the end and instead just `return` the created promise, so that you'd call `getReportData(…).then(mySuccessFunction)` instead of `getReportData(…, mySuccessFunction)` – Bergi Dec 10 '18 at 08:55
  • @Bergi, I've modified my code as shown in the Edit portion, but am still getting the same error. It just stops at the when statement. I am seeing it reporting that it at least entered getReportData with the correct passed parameters and claims XHR finished loading: GET for each of the three functions I am running. – BWieland86 Dec 11 '18 at 01:18
  • Did you solved it finally ? – Common Man Nov 12 '22 at 09:06

0 Answers0