Duplicates in a repeater are not allowed

chr_kluge
ServiceNow Employee
ServiceNow Employee

Hello together

I'm trying to do a lab self-paced and I don't get what is asked of me.

Server Side Script:

var table = options.datasource;

data.fields = $sp.getListColumns(table, "ess");

data.labels = [];

data.registrations = [];

if(!input){

        var regGR = new GlideRecord(table);

        regGR.orderByDesc("sys_created");

        data.labels = $sp.getFields(regGR, data.fields.toString());

        regGR.query();

        while(regGR.next()) {

            if($sp.canReadRecord(regGR)) {

                  var reg = {};

                  $sp.getRecordDisplayValues(reg, regGR, data.fields.toString());

                  $sp.getRecordValues(reg, regGR, 'sys_id');

                  data.registrations.push(reg);

            }

        }

}

The corresponding HTML is:

<div class="container">

  <div class="table-responsive">    

      <table class="table">

              <thead>

                  <tr>

                      <th ng-repeat="label in c.data.labels">{{label.label}}</th>

                  </tr>

              </thead>

              <tbody>

                  <tr ng-repeat="recs in c.data.registrations track by recs.sys_id" >

                      <td ng-repeat="field in c.data.fields" >{{recs[field]}}</td>

                  </tr>

              </tbody>

          </table>

    </div>

</div>

According to my Lab Guide this should work. But still I get the error message:

js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:11973 Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: field in c.data.fields, Duplicate key: string:_, Duplicate value: _

http://errors.angularjs.org/1.5.8/ngRepeat/dupes?p0=field%20in%20c.data.fields&p1=string%3A_&p2=_

      at js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:6202

      at ngRepeatAction (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:17755)

      at $watchCollectionAction (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:14284)

      at Scope.$digest (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:14354)

      at Scope.$apply (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:14449)

      at done (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:11104)

      at completeRequest (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:11216)

      at XMLHttpRequest.requestLoaded (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Aug_29_01_40_04_PDT_2017&c=2_42:11171)

Any ideas what I need to change?

Thank you very much

Christina

1 ACCEPTED SOLUTION

jesseadams
ServiceNow Employee
ServiceNow Employee

Ah, okay.


Based on your screenshot I think this is happening because fields is a string instead of an array.


ng-repeat will expect an array but it looks like you're using it on a string that is a comma separated list.


You can use fields = fields.split(","); to convert that string into an array.


View solution in original post

4 REPLIES 4

jesseadams
ServiceNow Employee
ServiceNow Employee

It looks like there are duplicate items in your fields array. Using ng-repeat with duplicate data will cause an error.


Use track by in your repeat to avoid it. You're already using tarck by in this repeater so just use that as a reference for how to do it.


<tr ng-repeat="recs in c.data.registrations track by recs.sys_id" >


chr_kluge
ServiceNow Employee
ServiceNow Employee

Hi Jesse



As you can see above I already tried track by recs.sys_id.



If I log the data object to the console I get the following:


dataObject.tiff



I don't see duplicate entries in there.


jesseadams
ServiceNow Employee
ServiceNow Employee

Ah, okay.


Based on your screenshot I think this is happening because fields is a string instead of an array.


ng-repeat will expect an array but it looks like you're using it on a string that is a comma separated list.


You can use fields = fields.split(","); to convert that string into an array.


chr_kluge
ServiceNow Employee
ServiceNow Employee

Hi Jesse



Wow, okay that did the trick. That was because I did the lab challenge that told me to use




data.fields = $sp.getListColumns(table, "ess");



instead of hard coded values like


data.fields = ["u_first_name", "u_last_name", ...];



So it work now setting:


data.fields = $sp.getListColumns(table, "ess").split(",");



Thank you very much


Christina