'subscribe' 在 Angular 5 中从服务返回 Observable<Object[]> 时出现未定义错误?
'subscribe' undefined error when returning Observable<Object[]> from service in Angular 5?
我正在为外部服务返回的每个值构建对象 new Azureblob(blob.name
,并将其添加到数组 new Azureblob(blob.name
。也可以从匿名函数 return localArr
中 return。
问题:
- 我们如何return通过组件使用它的方法?
- 还需要将其设为
Observable<Azureblob[]
,因为服务需要一段时间才能从服务器获取数据?
型号
export class Azureblob {
blobName: string;
constructor(private blobName1: string) {
this.blobName = blobName1;
}
}
服务。这是正确的吗?
import { Azureblob } from '../models/azureblob';
..
export class BlobService {
constructor() { }
blobServiceObj: any;
blobList: Observable<Azureblob[]> = of([]);
getAllBlobsJS(): Observable<Azureblob[]> {
var localArr: Azureblob[] = [];
this.blobServiceObj = AzureStorageBlobServiceJS.createBlobService(this.connectionString);
this.blobList = this.blobServiceObj.listBlobsSegmented('acs', null, function (error, results) {
if (error) {
console.log("**** Error");
} else {
for (var i = 0, blob; blob = results.entries[i]; i++) {
console.log("Blob ", i, blob); /** SEE BELOW **/
localArr.push(new Azureblob(blob.name));
}
}
console.log("localArr - # of blobs returned=", localArr.length); /** SEE BELOW **/
return localArr;
});
return this.blobList;
}
该服务工作正常,return结果为
Blob 0
BlobResult {name: "Git-Logo-1788C.png", creationTime: "Mon, 17 Sep 2018 17:57:39 GMT", lastModified: "Mon, 17 Sep 2018 17:57:39 GMT", etag: "0x8D61CC70ED10A9F", contentLength: "5684", …}
localArr - # of blobs returned= 4
组件
blobList: Azureblob[] = [];
this.blobService.getAllBlobsJS()
.subscribe(
(val) => {
console.log("..values=", val);
});
我看到错误
getAllBlobsJS
应该 return 作为 observable 以便您可以订阅它。
getAllBlobsJS(): Observable<Azureblob[]> {
return new Observable(obs=>{
var localArr: Azureblob[] = [];
this.blobServiceObj =
AzureStorageBlobServiceJS.createBlobService(this.connectionString);
this.blobList = this.blobServiceObj.listBlobsSegmented('acs', null, function
(error, results) {
if (error) {
console.log("**** Error");
obs.error();
} else {
for (var i = 0, blob; blob = results.entries[i]; i++) {
console.log("Blob ", i, blob); /** SEE BELOW **/
localArr.push(new Azureblob(blob.name));
}
}
console.log("localArr - # of blobs returned=", localArr.length); /** SEE BELOW
**/
return localArr;
});
obs.next(this.blobList);
obs.complete();
})}
我正在为外部服务返回的每个值构建对象 new Azureblob(blob.name
,并将其添加到数组 new Azureblob(blob.name
。也可以从匿名函数 return localArr
中 return。
问题:
- 我们如何return通过组件使用它的方法?
- 还需要将其设为
Observable<Azureblob[]
,因为服务需要一段时间才能从服务器获取数据?
型号
export class Azureblob {
blobName: string;
constructor(private blobName1: string) {
this.blobName = blobName1;
}
}
服务。这是正确的吗?
import { Azureblob } from '../models/azureblob';
..
export class BlobService {
constructor() { }
blobServiceObj: any;
blobList: Observable<Azureblob[]> = of([]);
getAllBlobsJS(): Observable<Azureblob[]> {
var localArr: Azureblob[] = [];
this.blobServiceObj = AzureStorageBlobServiceJS.createBlobService(this.connectionString);
this.blobList = this.blobServiceObj.listBlobsSegmented('acs', null, function (error, results) {
if (error) {
console.log("**** Error");
} else {
for (var i = 0, blob; blob = results.entries[i]; i++) {
console.log("Blob ", i, blob); /** SEE BELOW **/
localArr.push(new Azureblob(blob.name));
}
}
console.log("localArr - # of blobs returned=", localArr.length); /** SEE BELOW **/
return localArr;
});
return this.blobList;
}
该服务工作正常,return结果为
Blob 0
BlobResult {name: "Git-Logo-1788C.png", creationTime: "Mon, 17 Sep 2018 17:57:39 GMT", lastModified: "Mon, 17 Sep 2018 17:57:39 GMT", etag: "0x8D61CC70ED10A9F", contentLength: "5684", …}
localArr - # of blobs returned= 4
组件
blobList: Azureblob[] = [];
this.blobService.getAllBlobsJS()
.subscribe(
(val) => {
console.log("..values=", val);
});
我看到错误
getAllBlobsJS
应该 return 作为 observable 以便您可以订阅它。
getAllBlobsJS(): Observable<Azureblob[]> {
return new Observable(obs=>{
var localArr: Azureblob[] = [];
this.blobServiceObj =
AzureStorageBlobServiceJS.createBlobService(this.connectionString);
this.blobList = this.blobServiceObj.listBlobsSegmented('acs', null, function
(error, results) {
if (error) {
console.log("**** Error");
obs.error();
} else {
for (var i = 0, blob; blob = results.entries[i]; i++) {
console.log("Blob ", i, blob); /** SEE BELOW **/
localArr.push(new Azureblob(blob.name));
}
}
console.log("localArr - # of blobs returned=", localArr.length); /** SEE BELOW
**/
return localArr;
});
obs.next(this.blobList);
obs.complete();
})}