我正在尝试使用 gridsome-plugin-firestore 插件 ( https://gridsome.org/plugins/gridsome-source-firestore )。我想使用该插件连接到一个名为 news 的简单 firestore 数据库集合。新闻有多个不同领域的文档:
- 内容
- 发布日期
- 总结
- 作者
- 标题
- 等等
有谁知道我应该如何设置 gridsome.config 文件以使用 gridsome-plugin-firestore 插件访问此集合?。我无法从给出的说明中弄清楚。
最佳答案
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
关于firebase - 无法弄清楚如何使用 Gridsome-Plugin-Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129778/