Understanding the forEach container in SPFX

Occasional Contributor

I need to understand how to build our own webparts in SharePoint using the SharePoint Framework. I've bought a book on the subject and have been trying to make sense of it.

I'm struggling with trying to make sense of sample code from the SharePoint Development with SPFX by Jussi Roine and Olli Jääskeläinen . I have added an Interface for Towns, created mock data and then attempted to consume this mock data within the render() function.

I have diligently copied the manner in which the example code has been written. I created the Interface **ITowns.ts** with the following code:
export interface ITowns{

Then I created the mock data file, which I called **mdfTowns.ts** (for Mock Data File) with the following code:
import { ITowns } from "./ITowns";
export default class mdfTowns {
private static _lstTowns:ITowns[]=[
public static get(restUrl:string, options?:any)
return new Promise<ITowns[]>((resolve) =>{

In the webpart's class definition I have then created the following:
//Mocking Data File for Towns
private _getMDFTownsData():Promise<ITowns[]> {
return mdfTowns.get("")
.then((data: ITowns[])=> {
return data;
//Get the MockData from the ITowns interface
private _getTownLists(): Promise<ITowns[]> {
if (Environment.type === EnvironmentType.Local) {
return this._getMDFTownsData();
} else {
alert ("Todo #2");
return null;

So far, so good - right? I don't get any errors, everything is exactly like the book describes. I even went so far as to embed all of this within the working code from the book.

Within the render() function I then created a variable to hold the information of the towns in and populated it with data from the mock data as follows:

let l: string = "";
this._getTownLists().then(lstTowns => {
lstTowns.forEach(ListedTown => {
l += `
<li>${ListedTown.id} - ${ListedTown.Title}</li>

I then render this as follows:

this.domElement.innerHTML = `
<h3>List items</h3>
<H3>List of Towns</H3>

I use ${l.length} to count the number of towns I'm supposed to receive because ${l} on its own returns nothing.

The workbench returns the following:
List items
1 - First list item
2 - Second list item
3 - Third list item
4 - Fourth list item
5 - Fifth list item
6 - Sixth list item
7 - Seventh list item
8 - Eight list item
9 - Ninth list item
**List of Towns

The bold section (List of Towns) followed by the number 0 says that I'm doing something wrong - but I don't know what that is!

I'm obviously not understanding an important aspect of the forEach loop. I'm obviously doing something wrong, but the book doesn't help at all, and I cannot figure out how this should work.

I'm including the webpart file here as a txt file.

I've been banging my head against this for 2 days now and I can't see what I'm doing wrong. Please help...

[1]: /answers/storage/attachments/108295-simplepartwebpart.txt

2 Replies

I am back from my vacation in two days and will gladly look into it!
Please comment on this thread (ping me) if I have not answered in the following week.

Yours sincerely,
Aref Halmstrand

While im "AFK", could you try something for me, in mdfTowns.ts, in row 16, could you bring that row up to 15? So just hit backspace, see images below: 





Yours sincerely,
Aref Halmstrand