【问题标题】:Vue Js & Firebase relational query problemVue Js & Firebase 关系查询问题
【发布时间】:2020-04-10 21:29:18
【问题描述】:

我正在使用 Vue Js 和 Firebase 创建一个项目。从两个表中查询关系数据时遇到一些问题。我已经对 SQL 数据库关系系统有所了解。但由于 Firebase 是一个 NoSQL 数据库,所以不知道如何解决它。

表 1 客户
-> 身份证
-> 名称

表 2 销售
-> 身份证
-> 客户名称
-> 日期

这是我对 .vue 文件所做的查询

  let ref = db.collection('sales').where("created_month", "==", moment().format('MM-YYYY'))
                                  .orderBy('timestamp', 'desc')

  ref.onSnapshot(snapshot => {
    snapshot.docChanges().forEach(change => {
      if(change.type == 'added'){
        let doc = change.doc
        this.sales.push({
          id:doc.id,
          item_name:doc.data().item_name,
          price:doc.data().price,
          timestamp:moment(doc.data().timestamp).format('ll')
        })
      }
    })
  })

我的问题是如何使用客户表中的客户 ID 检索客户名称并显示在销售表上。

【问题讨论】:

  • 您的销售文档中有客户 ID 吗?看来您已经拥有customer_name。还是您实际上在此字段中存储了客户 ID?
  • 我在销售文件中有客户 ID。现在如何根据客户 ID 检索客户名称并将其显示在销售中
  • “如何根据客户 ID 检索客户名称并将其显示在销售中”-> 请查看我的答案:恕我直言,它回答了这个问题。您会在推入 sales 数组的每个对象中获得 customer_name。然后只需显示sales 数组,例如v-for (vuejs.org/v2/guide/#Conditionals-and-Loops)

标签: firebase vue.js google-cloud-platform google-cloud-firestore


【解决方案1】:

由于你的数据模型如下

Document in Collection "Customers"
 -> id
 -> name

Document in Collection "Sales"
 -> id
 -> customer_name
 -> date

我假设客户 ID 实际上存储在销售文档的 customer_name 字段中。

因此,您需要在侦听器中使用get() 方法获取客户文档。以下应该可以解决问题。

  const ref = db.collection('sales').where("created_month", "==", moment().format('MM-YYYY')).orderBy('timestamp', 'desc')

  ref.onSnapshot(snapshot => {
    snapshot.docChanges().forEach(change => {
      if(change.type == 'added'){
        let doc = change.doc;

        const customerId = doc.data().customer_name;
        const customerDocRef = db.collection('customers').doc(customerId);

        customerDocRef.get()
        .then(doc => {
           const customer_name = doc.data().name;  // Here we get the value of customer_name

          this.sales.push({
            id:doc.id,
            item_name:doc.data().item_name,
            price:doc.data().price,
            timestamp:moment(doc.data().timestamp).format('ll'),
            customer_name: customer_name  //Here we add the customer_name in each object that is pushed into the sales array
          })
      }
    })
  })

话虽如此,NoSQL 数据库中的一种经典方法是对数据进行非规范化处理,使您的查询能够轻松快速地执行。

在您的情况下,这意味着当您保存新的销售时,您直接在销售文档中写入 customer_idcustomer_name

我建议您看看这个 Firebase 视频,其中很好地解释了这种方法:https://www.youtube.com/watch?v=v_hR4K4auoQ。您还可以阅读这篇关于 NoSQL 数据建模方法的“著名”帖子:https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/

【讨论】:

  • @ShakilZaman 嗨,您有时间查看建议的解决方案吗?
【解决方案2】:

可以只使用 filter() 或 find() 吗?

let userSales = []
this.sales.forEach( sale => {
const costumer = costumers.find( costumer => costumer.id === sale.id)
userSales.push(costumer)
}

或者只是将销售集合放在用户集合中。

【讨论】:

    【解决方案3】:

    尝试以下方法:

      let ref = db.collection('customers').where("id", "==", id);
      ref.get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc.data());
                let customerName = doc.data().name;
                let salesRef     = db.collection('sales').doc('some-id');
                salesRef.set({
                       customerName : customerName,
                       date: "date",
                       id: "id"
                      }).then(function() {
                           console.log("Document successfully written!");
                      }).catch(function(error) {
                           console.error("Error writing document: ", error);
                      });
            });
        })
        .catch(function(error) {
            console.log("Error getting documents: ", error);
        });
    

    添加对集合customers 的引用,然后使用查询where 可以检索客户名称。然后您可以将该名称添加到 sales 文档中

    【讨论】:

    • 感谢您的回复。这是我的完整代码:codeshare.io/29qBNK。你能建议我现在做什么吗?
    猜你喜欢
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多