Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 当我尝试将2个map()RxJS操作符链接到管道()中时,为什么会出现此错误?_Angular_Typescript_Rxjs_Observable_Angular Observable - Fatal编程技术网

Angular 当我尝试将2个map()RxJS操作符链接到管道()中时,为什么会出现此错误?

Angular 当我尝试将2个map()RxJS操作符链接到管道()中时,为什么会出现此错误?,angular,typescript,rxjs,observable,angular-observable,Angular,Typescript,Rxjs,Observable,Angular Observable,在角度项目中,我定义了以下两种方法: acceptArtistBid(bid: Bid): Observable<Bid[]> { console.log("acceptArtistBid() start !!!"); // define a variable to hold the list of bids let listOfBids: Array<Bid>; // notice that the Observa

在角度项目中,我定义了以下两种方法:

acceptArtistBid(bid: Bid): Observable<Bid[]> {
    console.log("acceptArtistBid() start !!!");

    // define a variable to hold the list of bids
    let listOfBids: Array<Bid>;

    // notice that the Observable is returned by this method and not subscribed in the service
    return this.findArtistBidsAppliedByCurrentWall(bid).pipe(
        // here you use the map of Observable to transform the list, and within this map you call the map Array method to run the transformation on each bid
        map(artistsBisdList => {
            return listOfBids = artistsBisdList.map(bid => {
                bid["test"] = "TEST";
                console.log("CURRENT BID: ", bid);
                return bid;
            })

        }),

        /* Assume updateFirestore$ is a method that returns an Observable representing the update operation.
           This second map operator returns an array of Observables
         */
        map(modiefiedListOfBids => modiefiedListOfBids.map(updatedBid => {
            console.log("UPDATED BID: ", updatedBid);
            // UPDATED updatedBid OBJECT ON FIRESTORE
        }));
        )
}

findArtistBidsAppliedByCurrentWall(bid): Observable<Bid[]> {
    return this.db.collection('bids',
        ref => ref.where("wallId", "==", bid.wallId))
        .snapshotChanges()
        .pipe(
            map(snaps => {
                const courses = this.convertSnaps<Bid>(snaps);
                return courses;
            })
        )
}
IDE给了我以下错误:

Type 'Observable<void[]>' is not assignable to type 'Observable<Bid[]>'.
  Type 'void[]' is not assignable to type 'Bid[]'.
    Type 'void' is not assignable to type 'Bid'.ts(2322)
类型“Observable”不可分配给类型“Observable”。
类型“void[]”不能分配给类型“Bid[]”。
类型“void”不可分配给类型“Bid”。ts(2322)

怎么了?我错过了什么?如何修复它?

您必须在modifiedListofBids.map函数中返回“Bid[]”类型的对象

map(modiefiedListOfBids => modiefiedListOfBids.map(updatedBid => {
console.log("UPDATED BID: ", updatedBid);

// UPDATED updatedBid OBJECT ON FIRESTORE

return updatedBid; //return the modified object
}));

您的第二个
map
返回
void
,您应该将其更改为:

map(modiefiedListOfBids => modiefiedListOfBids.map(updatedBid => {
    console.log("UPDATED BID: ", updatedBid);
    return updatedBid;
}));

该方法需要一个
可观察的
,而您返回了一个
可观察的
(不返回任何内容)。这就是为什么会出现此错误。

现在您不会从
modifiedListofBids.map
的回调返回任何内容。阅读TypeScript提供给您的信息!如果映射的唯一原因是记录它,那么最好使用
tap
是的,但我假设最终目的是映射,所以我将映射保留在我的answer@PMO1948小心,
Array#map
Observable#map
不是一回事,也没有
Array#tap
这样的东西。您的建议是将
数组#map
的实例替换为
点击
。那肯定行不通。