首页 > 解决方案 > Angular在没有列表的情况下在firestore中添加新文档

问题描述

我是 angular 和 firebase 的新手,我有一个 crud 操作项目,如下所示

export class EmployeeService {
  employeeList: AngularFireList<any>;
  selectedEmployee: Employee = new Employee();
  constructor(private firebase: AngularFireDatabase) { }

  getData() {
    this.employeeList = this.firebase.list('employees');
    return this.employeeList;
  }

  insertEmployee(empoloyee: Employee) {
    this.employeeList.push({
      name: empoloyee.name,
      position: empoloyee.position,
      office: empoloyee.office,
      salary: empoloyee.salary
    });
  }

  updateEmployee(emp : Employee){
     this.employeeList.update(emp.$key,{
       name : emp.name,
       position : emp.position,
       office : emp.office,
       salary : emp.salary
     })
  }

  deleteEmployee(key : string){
    this.employeeList.remove(key);
  }

}

这里根据我的理解添加一条新记录,它将项目添加到通过 getData 获取的列表中。

是否可以在不获取列表的情况下在集合中添加新文档

谢谢

标签: node.jsangularfirebasegoogle-cloud-firestoreangularfire2

解决方案


您没有获取任何数据。

this.firebase.list('employees')是一个列表绑定。仅当您将valueChanges()和添加subscribe()到由valueChanges()


推荐阅读