Angular 角度7-对象组合和阵列

Angular 角度7-对象组合和阵列,angular,Angular,我有一个Userc型号(姓名、电话、电子邮件等) 和另一个型号的Usercco(相同,还有一个值coname(公司名称) 在Userc中,我有(coid:“公司id”) 在DB(firebase)中,我只有Userc和Company 我尝试合并所有Userc和Company.coname以获得Userco的列表: 组件中的 ngOnInit() { this.usercService.getAllUserc().then( (userc: Userc[]) => {

我有一个Userc型号(姓名、电话、电子邮件等) 和另一个型号的Usercco(相同,还有一个值coname(公司名称)

在Userc中,我有(coid:“公司id”) 在DB(firebase)中,我只有Userc和Company

我尝试合并所有Userc和Company.coname以获得Userco的列表:

组件中的

  ngOnInit() {
    this.usercService.getAllUserc().then(
      (userc: Userc[]) => {
        this.usercs = userc;

      });

      this.usercco= this.usercService.AddConametoUsercs(this.usercs)
      console.log(this.usercco);

  }
在UsercService中(这一个有效):

 getAllUserc() {
        return new Promise(
            (resolve, reject) => {
                firebase.database().ref('userc/').once('value').then(
                    (data: DataSnapshot) => {
                        resolve(data.val());
                    }, (error) => {
                        reject(error);
                    }
                );
            }
        );
    }
AddConametoUsercs(usercs: Userc[]) {
        var size= usercs.length;
        console.log('Taille : '+size);
        for (let i = 0; i < usercs.length; i++) {
            this.companyService.getCompany(usercs[i].coid).then(
                (company: Company) => {
                    this.company = company;
                    this.usercco[i].coname = this.company.coname;
                }
            );
            this.usercco[i].coid = usercs[i].coid;
            this.usercco[i].usercid = usercs[i].usercid;
            this.usercco[i].usercfname = usercs[i].usercfname;
            this.usercco[i].userclname = usercs[i].userclname;
            this.usercco[i].usercemail = usercs[i].usercemail;
            this.usercco[i].role = usercs[i].role;
        }
        return this.usercco;
    }
另一个(不起作用):

 getAllUserc() {
        return new Promise(
            (resolve, reject) => {
                firebase.database().ref('userc/').once('value').then(
                    (data: DataSnapshot) => {
                        resolve(data.val());
                    }, (error) => {
                        reject(error);
                    }
                );
            }
        );
    }
AddConametoUsercs(usercs: Userc[]) {
        var size= usercs.length;
        console.log('Taille : '+size);
        for (let i = 0; i < usercs.length; i++) {
            this.companyService.getCompany(usercs[i].coid).then(
                (company: Company) => {
                    this.company = company;
                    this.usercco[i].coname = this.company.coname;
                }
            );
            this.usercco[i].coid = usercs[i].coid;
            this.usercco[i].usercid = usercs[i].usercid;
            this.usercco[i].usercfname = usercs[i].usercfname;
            this.usercco[i].userclname = usercs[i].userclname;
            this.usercco[i].usercemail = usercs[i].usercemail;
            this.usercco[i].role = usercs[i].role;
        }
        return this.usercco;
    }
AddConametoUsercs(usercs:Userc[]){
var size=usercs.length;
控制台日志('Taille:'+大小);
for(设i=0;i{
这个公司=公司;
this.usercco[i].coname=this.company.coname;
}
);
this.usercco[i].coid=usercs[i].coid;
this.usercco[i].usercid=usercs[i].usercid;
this.usercco[i].usercfname=usercs[i].usercfname;
this.usercco[i].userclname=usercs[i].userclname;
this.usercco[i].usercemail=usercs[i].usercemail;
this.usercco[i].role=usercs[i].role;
}
返回this.usercco;
}
如何将Userc数据与Company.coname合并到Usercco中

我知道长度不起作用,因为它是一个对象而不是数组。那么如何获取每个Userc[]项呢?没有长度函数


感谢您的帮助^^d((^o^)/)

组件中已出现问题

 ngOnInit() {
    this.usercService.getAllUserc().then(
      (userc: Userc[]) => {
        this.usercs = userc;

      });

      this.usercco= this.usercService.AddConametoUsercs(this.usercs)
      console.log(this.usercco);

  }
在调用
AddConametoUsercs
方法之前,您没有等待
getAllUserc
承诺解析。在解析函数中,您正在设置
userc
值,但在解析之前,您正在调用
AddConametoUsercs
方法

解决这个问题的一个简单方法是在promise中指定
usercco

ngOnInit() {
    this.usercService.getAllUserc().then(
      (userc: Userc[]) => {
        this.usercs = userc;

      });

      this.usercco= this.usercService.AddConametoUsercs(this.usercs)
      console.log(this.usercco);

  }
你在用户服务中的
AddConametoUsercs
遇到了一些问题,对我来说,一个重大的危险信号是在for循环中使用承诺。据我所知,你只是希望
Userc
包含一个
companyname
属性,该属性恰好位于firebase中的另一个地方,并通过
g获取etCompany

使用
async-await
可以非常优雅地解决这个问题,但我敦促您在开始使用
async-await
之前更好地掌握承诺。不过,这是一个具有良好承诺的解决方案

解决方案

首先,我们希望在进行任何合并之前获得所有用户的所有公司名称。我们将通过循环用户,返回他们的
coid
并调用
getCompany
来完成此操作

// First step
//
AddConametoUsercs(usercs: Userc[]) {
  /** This array will contain all the company ids */
  const companyIds: number[] = usercs.map(userc => userc.coid);
}
在那之后,我们想用
Promise.all
做一些类似于循环的事情。这需要一个promises数组作为输入。在这种情况下,它将是
getCompany
返回的一个promises数组

AddConametoUsercs(usercs: Userc[]) {
  /** This array will contain all the company ids */
  const companyIds: number[] = usercs.map(userc => userc.coid);
  /** This is an array of promises */
  const companyPromises: Promise<Company>[] = companyIds.map( companyId => this.companyService.getCompany(companyId) );

  const usersWithCompanyNamePromise = Promise.all(companyPromises)
    .then( companies => {
      // companies[0] would match the company with the id matching companyIds[0]
      // which corresponds with the first user in usercs (usercs[0])

     /** We want to loop through our companies, and return a user with the 
      * company name
      */
      const usersWithCompanyName = companies.map( (company, index) => {
        /** 
        * This is the magical part, because the companies were fetched in the 
        * same order as the users
        */
        const correspendingUser = usercs[index];

        const userWithCompanyName = {
          // Using the object spread operator to put the user data here
          ...correspendingUser,
          coname: company.name
        }
        // Returning the new structure, which will end up in usersWithCompanyName
        //
        return userWithCompanyName;
      });

      return usersWithCompanyName;
    });

  return usersWithCompanyNamePromise;
}
AddConametoUsercs(usercs:Userc[]){
/**此数组将包含所有公司ID*/
const companyId:number[]=usercs.map(userc=>userc.coid);
/**这是一系列的承诺*/
const companyPromises:Promise[]=companyIds.map(companyId=>this.companyService.getCompany(companyId));
const userswithcompanynameconomise=Promise.all(companyPromises)
。然后(公司=>{
//公司[0]将与id匹配的公司id[0]相匹配
//它与usercs中的第一个用户(usercs[0])相对应
/**我们希望通过我们的公司进行循环,并返回一个用户
*公司名称
*/
const usersWithCompanyName=companies.map((公司,索引)=>{
/** 
*这是神奇的一部分,因为这些公司是在
*与用户的顺序相同
*/
const correspendingUser=usercs[index];
const userWithCompanyName={
//使用对象扩展操作符将用户数据放在此处
…通信员,
coname:company.name
}
//返回新结构,该结构将以usersWithCompanyName结尾
//
返回userWithCompanyName;
});
返回usersWithCompanyName;
});
返回用户SwithCompanyNamePromise;
}

组件中已出现问题

 ngOnInit() {
    this.usercService.getAllUserc().then(
      (userc: Userc[]) => {
        this.usercs = userc;

      });

      this.usercco= this.usercService.AddConametoUsercs(this.usercs)
      console.log(this.usercco);

  }
在调用
AddConametoUsercs
方法之前,您没有等待
getAllUserc
承诺解析。在解析函数中,您正在设置
userc
值,但在解析之前,您正在调用
AddConametoUsercs
方法

解决这个问题的一个简单方法是在promise中指定
usercco

ngOnInit() {
    this.usercService.getAllUserc().then(
      (userc: Userc[]) => {
        this.usercs = userc;

      });

      this.usercco= this.usercService.AddConametoUsercs(this.usercs)
      console.log(this.usercco);

  }
你在用户服务中的
AddConametoUsercs
遇到了一些问题,对我来说,一个重大的危险信号是在for循环中使用承诺。据我所知,你只是希望
Userc
包含一个
companyname
属性,该属性恰好位于firebase中的另一个地方,并通过
g获取etCompany

使用
async-await
可以非常优雅地解决这个问题,但我敦促您在开始使用
async-await
之前更好地掌握承诺。不过,这是一个具有良好承诺的解决方案

解决方案

首先,我们希望在进行任何合并之前获得所有用户的所有公司名称。我们将通过循环用户,返回他们的
coid
并调用
getCompany
来完成此操作

// First step
//
AddConametoUsercs(usercs: Userc[]) {
  /** This array will contain all the company ids */
  const companyIds: number[] = usercs.map(userc => userc.coid);
}
在那之后,我们想用
Promise.all
做一些类似于循环的事情。这需要一个promises数组作为输入。在这种情况下,它将是
getCompany
返回的一个promises数组

AddConametoUsercs(usercs: Userc[]) {
  /** This array will contain all the company ids */
  const companyIds: number[] = usercs.map(userc => userc.coid);
  /** This is an array of promises */
  const companyPromises: Promise<Company>[] = companyIds.map( companyId => this.companyService.getCompany(companyId) );

  const usersWithCompanyNamePromise = Promise.all(companyPromises)
    .then( companies => {
      // companies[0] would match the company with the id matching companyIds[0]
      // which corresponds with the first user in usercs (usercs[0])

     /** We want to loop through our companies, and return a user with the 
      * company name
      */
      const usersWithCompanyName = companies.map( (company, index) => {
        /** 
        * This is the magical part, because the companies were fetched in the 
        * same order as the users
        */
        const correspendingUser = usercs[index];

        const userWithCompanyName = {
          // Using the object spread operator to put the user data here
          ...correspendingUser,
          coname: company.name
        }
        // Returning the new structure, which will end up in usersWithCompanyName
        //
        return userWithCompanyName;
      });

      return usersWithCompanyName;
    });

  return usersWithCompanyNamePromise;
}
AddConametoUsercs(usercs:Userc[]){
/**