【问题标题】:Angular add new doc in firestore without listAngular在没有列表的情况下在firestore中添加新文档
【发布时间】:2018-08-01 09:55:53
【问题描述】:

我是 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.js angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    您没有获取任何数据。

    this.firebase.list('employees') 是一个列表绑定。仅当您将 valueChanges()subscribe() 添加到由 valueChanges() 创建的这个 observable 时,才会获取数据

    【讨论】:

    • 好的,我从昨天才开始学习 firebase 和 angular,如果这是通过 angular 使用 firebase 的推荐方法,请告诉我
    • 我认为没有推荐的方式从 angular 访问 firebase ...您可以使用本机 firebase js api,或者(像您一样)angularfire2。这不是一个使用这个或另一个,你可以将两者结合起来。 angularfire2 的The documentation 应该为您指明正确的方向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2019-04-12
    • 1970-01-01
    • 2010-11-03
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多