【问题标题】:How can i add a value/entry to a field within a collection in firebase如何将值/条目添加到firebase集合中的字段
【发布时间】:2020-02-19 13:54:20
【问题描述】:

Firebase DB

My code trying to addtodo error on console once i click add to do

只是想知道有没有人知道我将如何将我的 addtodo 存储在我的 todolist 字段中的 house 子集合中 `

todo_title:string;
  todo_description: string;
  todo_last_date: Date;
  todo_owner: string;

  constructor(private navCtrl: NavController, private toastCtrl:ToastController) { 
  this.todo_owner = firebase.auth().currentUser.uid;
  }
  ngOnInit() {

    firebase.firestore().collection("house").doc("this.house.id").collection("toDoList").add({
      title: this.todo_title,
      description: this.todo_description,
      last_date: this.todo_last_date,
      owner: this.todo_owner,
      created: firebase.firestore.FieldValue.serverTimestamp
      ()
    }).then((docRef) => {
      this.toastCtrl.create({
        message: "toDo has been added!",
        duration: 2000
      }).then((toast) => {
        toast.present();
        this.navCtrl.navigateBack
      })
       }).catch((err) => {
         this.toastCtrl.create({
           message: "ToDo has been Added",
           duration: 3000
         }).then((toast) => {
           toast.present();
         })
         })
       }

  }`

【问题讨论】:

    标签: angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    错误告诉你你传递给title的值(你传递给add()方法的对象的属性)是未定义的。所以,换句话说,this.todo_title 似乎是undefined


    另外,我认为你应该这样做:

    firebase.firestore().collection("house").doc(this.house.id).collection("toDoList").add(...)
    

    注意doc(this.house.id) 而不是doc("this.house.id"):使用后者你传递一个值为"this.house.id" 的字符串。

    【讨论】:

    • “AddTodoPage”类型上不存在属性“house”,当我将其更改为我在 vs 代码上收到此错误时。
    • @Keaner111 您必须调试代码并验证您使用的值是否正确。在我们的代码中,this 似乎不是您所期望的。
    猜你喜欢
    • 2013-11-26
    • 2020-11-12
    • 2022-11-30
    • 2016-09-01
    • 2018-03-29
    • 2016-12-29
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多