Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 如何返回可观察值<;数组[]>;方法?_Angular_Return_Observable - Fatal编程技术网

Angular 如何返回可观察值<;数组[]>;方法?

Angular 如何返回可观察值<;数组[]>;方法?,angular,return,observable,Angular,Return,Observable,我有这段代码,我没有尝试使用http get请求从数据库中获取用户列表,但该方法似乎给了我一个错误,即函数没有返回值,我不明白为什么 代码: listFriends():可观察{ this.serviceChat.list_users().subscribe((res)=>{ console.log(res) res.data.forEach(用户=>{ MyAdapter.userAdd={}; MyAdapter.userAdd.id=user.id; MyAdapter.userAdd.a

我有这段代码,我没有尝试使用http get请求从数据库中获取用户列表,但该方法似乎给了我一个错误,即函数没有返回值,我不明白为什么

代码:

listFriends():可观察{
this.serviceChat.list_users().subscribe((res)=>{
console.log(res)
res.data.forEach(用户=>{
MyAdapter.userAdd={};
MyAdapter.userAdd.id=user.id;
MyAdapter.userAdd.avatar=user.avatar;
MyAdapter.userAdd.status=user.status;
MyAdapter.userAdd.participantType=0;
MyAdapter.userAdd.displayName=user.displayName;
MyAdapter.mockedParticipants.push(MyAdapter.userAdd)
});        
console.log(MyAdapter.mockedParticipants)
返回(MyAdapter.mockedParticipants.map)(用户=>{
console.log(用户)
让participantResponse=新的participantResponse();
participanterresponse.participant=用户;
participantResponse.metadata={
TotalUnderMessages:Math.floor(Math.random()*10)
}
console.log(participantResponse)
返回参与者响应;
}));
}),
错误=>{
让participantResponse=新的participantResponse();
console.log(错误)
返回参与者响应
}; 
设置超时(()=>{
返回(MyAdapter.mockedParticipants.map)(Untete=>{
控制台日志(utente)
让participantResponse=新的participantResponse();
participantResponse.participant=utente;
participantResponse.metadata={
TotalUnderMessages:Math.floor(Math.random()*10)
}
console.log(participantResponse)
返回参与者响应;
}));     
}, 10000);   
}

以下是正确的实现-

   listFriends(): Observable<ParticipantResponse[]>{
          return  this.serviceChat.list_users().pipe(map(res => {
            console.log(res)
                  res.data.forEach(user => {
                  MyAdapter.userAdd={};
                  MyAdapter.userAdd.id=user.id;
                  MyAdapter.userAdd.avatar=user.avatar;
                  MyAdapter.userAdd.status=user.status;
                  MyAdapter.userAdd.participantType=0;
                  MyAdapter.userAdd.displayName=user.displayName;
                  MyAdapter.mockedParticipants.push(MyAdapter.userAdd)              
                });        
              console.log(MyAdapter.mockedParticipants)
           return MyAdapter.mockedParticipants.map(users=> {
            console.log(users)
            let participantResponse = new ParticipantResponse();
      
             participantResponse.participant = users;
             participantResponse.metadata = {
               totalUnreadMessages: Math.floor(Math.random() * 10)
            }
            console.log(participantResponse)
             return participantResponse;
          });
    }));

你订阅了一个函数,你期望返回一个可观察的,考虑重构你的代码,你没有指定它不定义的函数的返回,并且你可能想考虑重构它仍然不起作用,它总是犯同样的错误。“声明类型既不是'void'也不是'any'的函数必须返回值。”请添加
return
语句(函数的第一行)。请参阅我更新的答案。
   listFriends(): Observable<ParticipantResponse[]>{
          return  this.serviceChat.list_users().pipe(map(res => {
            console.log(res)
                  res.data.forEach(user => {
                  MyAdapter.userAdd={};
                  MyAdapter.userAdd.id=user.id;
                  MyAdapter.userAdd.avatar=user.avatar;
                  MyAdapter.userAdd.status=user.status;
                  MyAdapter.userAdd.participantType=0;
                  MyAdapter.userAdd.displayName=user.displayName;
                  MyAdapter.mockedParticipants.push(MyAdapter.userAdd)              
                });        
              console.log(MyAdapter.mockedParticipants)
           return MyAdapter.mockedParticipants.map(users=> {
            console.log(users)
            let participantResponse = new ParticipantResponse();
      
             participantResponse.participant = users;
             participantResponse.metadata = {
               totalUnreadMessages: Math.floor(Math.random() * 10)
            }
            console.log(participantResponse)
             return participantResponse;
          });
    }));
listFriends().subscribe((res)=>{
              // res is your converted response above
          }),
          error =>{
            let participantResponse = new ParticipantResponse();
            console.log(error)
            return participantResponse
          }; 
          setTimeout(() => {
            return of(MyAdapter.mockedParticipants.map(utente => {
              console.log(utente)
              let participantResponse = new ParticipantResponse();
        
               participantResponse.participant = utente;
               participantResponse.metadata = {
                 totalUnreadMessages: Math.floor(Math.random() * 10)
              }
              console.log(participantResponse)
               return participantResponse; 
              }));     
          }, 10000);   
     }