无法从Observable获取列表

时间:2019-06-27 09:32:08

标签: angular angular6

我想使用localStorage获取字符串列表,如:

this.userService.getExpertisesListByIdUser(2).map((listExpertises: Array<any>) =>
      {
          let result:Array<Expertise> = [];
          let libs:Array<String> = [];
          if (listExpertises)
          {
              listExpertises.forEach((expert) =>
              {
                  libs.push(expert.name);
                  result.push(new Expertise(expert.id, expert.name, expert.username));
              });
          }

          localStorage.removeItem("libs");
          localStorage.setItem("libs", JSON.stringify(libs));

      })

      var storedNames = JSON.parse(localStorage.getItem("libs"));
      console.log(storedNames.length);   // That gives 0 as length : WRONG

getExpertisesListByIdUser 方法是:

getExpertisesListByIdUser(id: number): Observable<any>
{
    return this.http.get(`${this.baseUrl}/users/expertises/${id}`);
}

经评论,我得到的长度为0。 如果将localStorage放在map外面,我得到正确的结果。 但是,我需要在map中使用它来获得同步

现在,此代码的大小正确。

this.userService.getExpertisesListByIdUser(2).subscribe(listExpertises => {

         let libs:Array<String> = [];
         let exps:Array<Expertise> = [];

         if (listExpertises)
         {
             listExpertises.forEach((expert) =>
             {
               libs.push(expert.name);
               exps.push(new Expertise(expert.id, expert.name, expert.selected));
             });
         }

         localStorage.removeItem("libs");
         localStorage.setItem("libs", JSON.stringify(libs));

         localStorage.removeItem("exps");
         localStorage.setItem("exps", JSON.stringify(exps));

      });

      var storedNames = JSON.parse(localStorage.getItem("libs"));
      var storedExperties = JSON.parse(localStorage.getItem("exps"));
      console.log("----------------- Size: " + storedNames.length);
      console.log(storedNames);

      storedNames.forEach((name: string) =>
      {
          console.log("--------------Name from list of strings: " + name);
      });

      storedExperties.forEach((exp: Expertise) =>
      {
          console.log("--------------Name from list of objects: " + exp.name);
      });

但是在控制台上我得到了

screenshot of empty list

你能告诉我我错过了什么吗?。

谢谢。

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用.subscribe()

this.userService.getExpertisesListByIdUser(2).subscribe(listExpertises => {
   let result:Array<Expertise> = [];
   let libs:Array<String> = [];
   if (listExpertises)
   {
       listExpertises.forEach((expert) =>
       {
         libs.push(expert.name);
         result.push(new Expertise(expert.id, expert.name, expert.username));
       });
   }

   localStorage.removeItem("libs");
   localStorage.setItem("libs", JSON.stringify(libs));

   var storedNames = JSON.parse(localStorage.getItem("libs"));
   console.log(storedNames.length);
});

.map()仅在要将管道功能添加到可观察对象时才应使用。同样,您不必在订阅函数内部定义响应类型。如果要定义响应类型,请在使用http客户端时执行

getExpertisesListByIdUser(id: number): Observable<yourTypeHere>
{
    return this.http.get<yourTypeHere>(`${this.baseUrl}/users/expertises/${id}`);
}

答案 1 :(得分:0)

使用.subscribe

像这样:

  this.userService.getExpertisesListByIdUser(2).subscribe((listExpertises: Array<any>) => {
    console.log(listExpertises)
    ... // rest of your code
  })