Reference.child失败:第一个参数是无效路径

时间:2018-02-07 15:32:03

标签: angular firebase angularfire angularfire2

当我使用参数从数据库获取对象时出现此错误:

enter image description here

代码组件:

 constructor(public employeeService:EmployeeService, public router:Router, public activatedRoute:ActivatedRoute) { }

      ngOnInit() {
        this.email = this.activatedRoute.snapshot.params['email'];
        this.employeeService.getEmployee(this.email).subscribe(employee => {
            if (employee.salary >0) { this.hasSalary = true;}
            this.employee = employee;
            console.log(this.employee);
        });
      }

代码服务:

   getEmployee(email:string)
   {
     return this.item = this.af.object('employeesdb/'+email).valueChanges() as FirebaseObjectObservable<Employee>; 
   }

2 个答案:

答案 0 :(得分:1)

您需要使用给定的电子邮件查询集合并获取对象。尝试

ngOnInit() {
    this.email = this.activatedRoute.snapshot.params['email'];
    this.employeeService.getEmployee(this.email).subscribe(employee => {
        employee.forEach(snapshot=>{
            console.log(snapshot);
        }
    });
  }

使用angularfire的查询方法

getEmployee(email:string)
   {
     return this.item = this.af.list('/employeesdb', ref => ref.orderByChild('email').equalTo(email)).valueChanges(); 
   }

答案 1 :(得分:0)

如错误所示,您的路径可能不包含点(。) 您使用电子邮件地址作为参数,显然包含一个点。 我建议为每位员工生成一个ID,并按照指定的ID获取员工。