【问题标题】:Use Firebase SDK with Netlify Lambda Functions将 Firebase 开发工具包与 Netlify Lambda 函数结合使用
【发布时间】:2019-01-02 11:20:36
【问题描述】:

我创建了一个使用 React + Firebase + Lambda 函数的项目。

我在前端有 Firebase 代码,我需要一些后端来处理一些事件。 (Prevent users from modifying data in Firebase but allow this data to be updated by the application)

当我使用 Netlify 部署我的应用程序时,我可以通过 netlify-lambda 访问 Amazon Lambda 函数。 (https://www.netlify.com/docs/functions/)

通常一切正常(mailchimp API、snipcart API 等...)

但我无法让 Firebase 正常工作。

我创建了一个具有读写权限的服务帐号。

这里是我的 lambda 函数的代码: (只是一个尝试查看数据库的用户部分的测试。)

import firebaseAdmin from 'firebase-admin'
const serviceAccount = require('../utils/FirebaseServiceAccountKey.json')

export function handler (event, context, callback) {
  firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: 'https://sample-3615.firebaseio.com'
  })

  const db = firebaseAdmin.database()
  const ref = db.ref('/users')
  let users = {}

  ref.once('value', function (snapshot) {
    console.log(snapshot.val())
    users = snapshot.val()
  })

  callback(null, {
    statusCode: 200,
    body: JSON.stringify({ users })
  })
}

它返回给我:TypeError: rtdb.initStandalone is not a function

我也有很多这样的警告:Module not found: Error: Can not resolve 'memcpy' 以及其他包。

我对组件中函数的调用:

handleClick = (e) => {
    e.preventDefault()

    this.setState({loading: true})
    fetch('/.netlify/functions/score')
      .then(res => res.json())
      .then(json => console.log(json.users))
      .then(() => this.setState({loading: false}))
  }

我不确定问题出在哪里。网页包?

【问题讨论】:

  • 你看到this answer了吗?
  • 好的,谢谢!我试过这个解决方案。我按照帖子中的说明修改了 webpack 配置,但不幸的是它不适用于 Gatsby。

标签: firebase aws-lambda netlify


【解决方案1】:

我无法使用来自 Netlify 的 AWS Lambda 运行开发工具包。

要使用来自 Netlify Lambda 函数的 Firebase,我将通过具有管理员权限的 REST API。

https://firebase.google.com/docs/reference/rest/database/

它就像那样完美地工作。

import { google } from 'googleapis'
import fetch from 'node-fetch'
const serviceAccount = require('../utils/FirebaseServiceAccountKey.json')

const scopes = [
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/firebase.database'
]

// Authenticate a JWT client with the service account.
const jwtClient = new google.auth.JWT(
  serviceAccount.client_email,
  null,
  serviceAccount.private_key,
  scopes
)

export function handler (event, context, callback) {
  const res = JSON.parse(event.body)
  // Use the JWT client to generate an access token.
  jwtClient.authorize(async function (error, tokens) {
    if (error) {
      console.log('Error making request to generate access token:', error)
    } else if (tokens.access_token === null) {
      console.log('Provided service account does not have permission to generate access tokens')
    } else {
      const accessToken = tokens.access_token
      const score = await fetch(`https://example-3615.firebaseio.com/scores/${res.uid}/score.json`)
        .then(data => data.json())
        .then(score => score + res.score)

      fetch(`https://example-3615.firebaseio.com/scores/${res.uid}.json?access_token=${accessToken}`, {
        body: JSON.stringify({ score, displayName: res.user.displayName, photoURL: res.user.photoURL }),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: 'PATCH'
      })
        .then(() => {
          callback(null, {
            statusCode: 200,
            body: 'Score +1'
          })
        })
    }
  })
}

【讨论】:

【解决方案2】:

问题出在“webpack.server.js”配置文件中。 netlify-lambda 用于捆绑服务器端代码(功能代码),由于某些原因,它不正确地捆绑它。 所以我将新文件添加到项目根“webpack.server.js”:

//webpack.config.js
const path = require('path');
const pkg = require('./package')
const GenerateJsonPlugin = require('generate-json-webpack-plugin')


const externals = [
    'firebase-admin'
]

const genPackage = () => ({
    name: 'functions',
    private: true,
    main: 'index.js',
    license: 'MIT',
    dependencies: externals.reduce(
    (acc, name) =>
        Object.assign({}, acc, {
        [name]:
            pkg.dependencies[name] ||
            pkg.devDependencies[name]
        }),
    {}
    )
})


module.exports = {
    target: 'node',
    resolve: {
        mainFields: ['module', 'main']
    },
    externals: externals.reduce(
        (acc, name) => Object.assign({}, acc, { [name]: true }),
        {}
    ),
    plugins: [new GenerateJsonPlugin('package.json', genPackage())]
}

此文件配置将创建一个新的 package.json 文件,放置在 lambda dist 中。

更新 查看我在 Medium (Firebase Admin with Netlify lambda functions) 上的帖子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2015-08-02
    • 2022-11-02
    • 2021-03-16
    相关资源
    最近更新 更多