In my TypeScript/Angular2/SPFx project, I have the following code snippet:
// Populate the regulatory documents dropdown
this.regulatoryDocumentsService.fetchRegulatoryDocumentsData().subscribe(
data => { this.regulatoryDocumentsData = data },
err => { window.console && console.log(err) }
);
Explaining further:
public fetchRegulatoryDocumentsData(): Observable<RegulatoryDocument[]> {
var choicesArray: Array<RegulatoryDocument> = new Array<RegulatoryDocument>();
// Handling local environment
if (Environment.type === EnvironmentType.Local)
{
// Providing dummy data for the form - it works
window.console && console.log("fetchRegulatoryDocumentsData(): Local workbench in use");
choicesArray.push(new RegulatoryDocument(1,"Document 1"));
choicesArray.push(new RegulatoryDocument(2, "Document 2"));
choicesArray.push(new RegulatoryDocument(3, "Document 3"));
return Observable.of<RegulatoryDocument[]>(choicesArray);
}
else if (Environment.type == EnvironmentType.SharePoint ||
Environment.type == EnvironmentType.ClassicSharePoint)
{
// Issue arises here when subscribing to fetchRegulatoryDoocumentsData() due to undefined
pnp.sp.web.lists.getByTitle('Regulatory Documents').items.get().then((choices: any[]) => {
window.console && console.log("choices ...");
window.console && console.log(choices);
if (choices) {
[].slice.call(choices).forEach(choice => {
choicesArray.push(new RegulatoryDocument(choice.ID, choice.Title));
});
}
return Observable.of<RegulatoryDocument[]>(choicesArray);
});
}
}
However, encountering an issue when
Environment.type != EnvironmentType.Local
, as a subscription error occurs mentioning inability to subscribe to undefined. The suspicion falls on the nested PNP promise structure. Any suggestions or insights would be highly appreciated.