【问题标题】:Can't Figure Out How to Use Gridsome-Plugin-Firestore不知道如何使用 Gridsome-Plugin-Firestore
【发布时间】:2020-03-26 12:37:56
【问题描述】:

我正在尝试使用 gridsome-plugin-firestore 插件 (https://gridsome.org/plugins/gridsome-source-firestore)。我想使用该插件连接到一个名为 news 的简单 firestore 数据库集合。新闻有许多不同领域的文档:

  • 内容
  • 发布日期
  • 总结
  • 作者
  • 标题

有谁知道我应该如何设置 gridsome.config 文件以使用 gridsome-plugin-firestore 插件访问此集合?我无法从给出的说明中弄清楚。

【问题讨论】:

    标签: firebase vue.js gridsome


    【解决方案1】:

    Gridsome 文档比 npm 版本更清晰一些,但您需要生成 Firebase Admin SDK 私钥并将整个文件下载到您的 Gridsome 应用程序中,并将其作为模块导入到 gridsome.config.js 中,随便命名您想要的选项 > 凭据:要求字段如下。

    首先,您需要 Firestore 插件

    $ yarn add gridsome-source-firestore
    

    然后在gridsome.config.js中

    const { db } = require('gridsome-source-firestore')
    
    module.exports = {
      plugins: [
        {
          use: 'gridsome-source-firestore',
          options: {
            credentials: require('./my-project-firebase-adminsdk-qw2123.json'), // 
            Replace with your credentials file you downloaded.
            debug: true, // Default false, should be true to enable live data updates
            ignoreImages: false, // Default false
            imageDirectory: 'fg_images', // Default /fg_images
            collections: [
              {
                ref: (db) => {
                  return db.collection('news')
                },
                slug: (doc, slugify) => {
                  return `/news/${slugify(doc.data.title)}`
                },
                children: [
                  {
                    ref: (db, parentDoc) => {
                      return parentDoc.ref.collection('posts')
                    },
                    slug: (doc, slugify) => {
                      return `/${slugify(doc.data.title)}`
                    },
                  }
                ]
              }
            ]
          }
        }
      ]
    }
    

    您可能需要根据您的数据库结构将“帖子”更改为“内容”并更改相应的页面查询以适应,Github 上的这个 Gridsome Firestore 启动器中有一些示例和其他有用的设置信息https://github.com/u12206050/gridsome-firestore-starter.git

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 1970-01-01
      • 2021-11-21
      • 2019-11-18
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多