0
        var dataObj = [];

        // Get data service

        VSS.getService(VSS.ServiceIds.ExtensionData)
            // the callback on the next line returns x` promise, which the JavaScript engine will follow, so you don't need to nest the next `then`
            .then((dataService) => dataService.getDocuments('MyCollection3'))
            .then((docs) => {
                // keep a reference to the element instead of searching for it in each loop.
                console.log(docs)
                tempi = 0;
                for (const doc of docs) {
                    dataObj[tempi] = {
                        name: doc.name,
                        projected: doc.projected,
                        actual: doc.actual,
                        le: doc.le,
                    }
                    tempi++;
                }
                console.log(dataObj)
            });


        var i = 0;

        console.log('fin', dataObj)

The VSS.getService() connects to a data storage provided by Microsoft's VSTS extension kit. I declare an empty array before the assignment and assign objects into the array inside my getService.

I get docs successfully, but when I print it out eventually it gives an empty array [].

How can I make it persist?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Dawn17
  • 7,825
  • 16
  • 57
  • 118

1 Answers1

0

Is there any reason you're using const in a loop?

for (const doc of docs) {

Because this means doc is always the original doc and can only ever be the original doc.

NotoriousPyro
  • 526
  • 3
  • 16