【问题标题】:Nuxt SPA dynamic routes based on Firebase Firestore data基于 Firebase Firestore 数据的 Nuxt SPA 动态路由
【发布时间】:2020-03-05 19:30:10
【问题描述】:

所以我想在 Netlify 上托管一个 nuxt 站点,其中有一个子路由,其 slug 是一个 firebase firestore 文档 ID。

例子:

https://www.example.com/users/steve

(其中“steve”是documentid)

因此,当路由被击中时,我需要查询 firebase 以查看它是否存在,如果不存在,我将不得不返回 404。这甚至可能吗?我可以在 .net 或 php 中轻松完成,但我对 SPA 非常不确定。

如果我能做到这一点,我具体应该在文档中寻找什么?

【问题讨论】:

  • 您好,我正面临和您一样的问题,在 Netlify 上托管了一个 SPA Nuxt 应用程序,我需要在动态路由上显示来自 Firebase 的数据。如果您能分享您的解决方案,我将不胜感激。使用给定的技术栈这是否可能?

标签: firebase google-cloud-firestore routing nuxt.js


【解决方案1】:

一种解决方案是实现HTTPS Cloud Function,您可以像 REST API 一样调用它,向函数端点发送 HTTP GET 请求。

正如文档“用作onRequest() 的参数,Request 对象使您可以访问客户端发送的 HTTP 请求的属性”中所述。

所以你的云函数看起来像:

exports.getUser = functions.https.onRequest((req, res) => {
  // get the value of the user by parsing the url
  const baseUrl = req.baseUrl;
  //Extract the user from baseUrl
  const user = ....

 //query the Firestore database
 admin.firestore().collection('users').doc(user).get()
 .then(doc => {
    if (doc.exists) {
        res.status(200).end();
    } else {
        res.status(404).end();
    }

});

有关 Cloud Functions 的更多信息,请参阅 get started pagevideo series

请注意,您可以connect an HTTP function to Firebase Hosting,这样“您的 Firebase 托管站点上的请求可以代理到特定的 HTTP 函数”。

【讨论】:

  • @SteveMcNiven-Scott 嗨,您有时间查看建议的解决方案吗?
  • 嗨,我正面临着确切的问题。我想试试你的答案,但是如果我的应用托管在 Netlify 上,我该如何编写 firebase 函数?
猜你喜欢
  • 2020-10-28
  • 2019-12-21
  • 2023-03-21
  • 2021-02-21
  • 2023-03-10
  • 2016-10-11
  • 1970-01-01
  • 2020-11-14
  • 2018-01-17
相关资源
最近更新 更多