【问题标题】:Firebase-Admin with Nuxt使用 Nuxt 的 Firebase 管理员
【发布时间】:2020-09-07 11:51:15
【问题描述】:

我正在尝试使用 Nuxt 使用 Firestore 数据库。但是下面的代码在编译时会产生错误。 我留下了我正在做的项目的一些摘录

在这个文件中我已经配置了数据库凭据路径

README.md

$env:GOOGLE_APPLICATION_CREDENTIALS="D:\Educacion\Proyectos\Go\social\whatsapp-f91a0.json"

nuxt.config.js

plugins: [
    '@plugins/firebase.js'
  ],

插件

firebase.js

var admin = require("firebase-admin");

var serviceAccount = require("whatsapp-f91a0.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://whatsapp-f91a0.firebaseio.com"
});

const db = admin.database();

export {db}

index.vue

<script>
import db from 'firebase';

  db.collection('categorias').doc('aaa').get()
  .then(doc=>{
    if (!doc.exists){
      console.log('no existe')
    }
  })
  .catch(err=>{
    console.error('error', err);
    process.exit();
  });
</script>

这里编译的时候报错

错误

 WARN  Compiled with 3 warnings                                                                                                    friendly-errors 17:54:45  


 WARN  in ./node_modules/hash-stream-validation/index.js                                                                           friendly-errors 17:54:45  

Module not found: Error: Can't resolve 'fast-crc32c' in 'D:\Educacion\Proyectos\Go\social\node_modules\hash-stream-validation'     friendly-errors 17:54:45
                                                                                                                                   friendly-errors 17:54:45

 WARN  in ./node_modules/retry-request/index.js                                                                                    friendly-errors 17:54:45  

Module not found: Error: Can't resolve 'request' in 'D:\Educacion\Proyectos\Go\social\node_modules\retry-request'                  friendly-errors 17:54:45
                                                                                                                                   friendly-errors 17:54:45  

 WARN  in ./node_modules/configstore/node_modules/write-file-atomic/index.js                                                       friendly-errors 17:54:45  

Module not found: Error: Can't resolve 'worker_threads' in 'D:\Educacion\Proyectos\Go\social\node_modules\configstore\node_modules\write-file-atomic'        
                                                                                                                                   friendly-errors 17:54:45  

 ERROR  Failed to compile with 20 errors                                                                                           friendly-errors 17:54:45  

These dependencies were not found:                                                                                                 friendly-errors 17:54:45
                                                                                                                                   friendly-errors 17:54:45  
* child_process in ./node_modules/google-auth-library/build/src/auth/googleauth.js                                                 friendly-errors 17:54:45  
* dns in ./node_modules/@grpc/grpc-js/build/src/resolver-dns.js                                                                    friendly-errors 17:54:45  

 WARN  in ./node_modules/configstore/node_modules/write-file-atomic/index.js                                                       friendly-errors 17:54:45  

Module not found: Error: Can't resolve 'worker_threads' in 'D:\Educacion\Proyectos\Go\social\node_modules\configstore\node_modules\write-file-atomic'        
                                                                                                                                   friendly-errors 17:54:45  

 ERROR  Failed to compile with 20 errors                                                                                           friendly-errors 17:54:45  

These dependencies were not found:                                                                                                 friendly-errors 17:54:45  
                                                                                                                                   friendly-errors 17:54:45  
* child_process in ./node_modules/google-auth-library/build/src/auth/googleauth.js                                                 friendly-errors 17:54:45  
* dns in ./node_modules/@grpc/grpc-js/build/src/resolver-dns.js                                                                    friendly-errors 17:54:45  
* fs in ./node_modules/@google-cloud/storage/build/src/file.js, ./node_modules/@grpc/grpc-js/build/src/tls-helpers.js and 8 others friendly-errors 17:54:45  
* http2 in ./node_modules/@grpc/grpc-js/build/src/server.js                                                                        friendly-errors 17:54:45  
* net in ./node_modules/@grpc/grpc-js/build/src/resolver-dns.js, ./node_modules/http-proxy-agent/dist/agent.js and 1 other         friendly-errors 17:54:45  
* tls in ./node_modules/@grpc/grpc-js/build/src/channel-credentials.js, ./node_modules/http-proxy-agent/dist/agent.js and 1 other  friendly-errors 17:54:46  
* whatsapp-f91a0.json in ./plugins/firebase.js  

有人可以帮我吗

【问题讨论】:

  • 该错误表明您的项目中没有安装一些依赖项,例如googleauth.jsresolver-dns.js等。您是否尝试过在构建期间重新安装依赖项?
  • @ralemos 您好,我已经安装了依赖项,但仍然出现错误。虽然在 firebase 页面上并没有提到它需要额外的依赖项

标签: javascript node.js firebase google-cloud-firestore nuxt.js


【解决方案1】:

Nuxt 是一个前端框架,旨在以多种方式(包括服务器端)提供内容。

Firebase-admin 是一个服务器端库。您可以在这里和那里进行一些调整,但根本不推荐这样做,因为您必须公开您的凭据。

Firebase Javascript SDKFirebase-Nuxt 结合使用(可选)与一组rules and auth claims 组合使用,这样您就可以以管理员身份工作。

【讨论】:

    【解决方案2】:

    执行此操作的一种方法是将应用程序初始化两次,一次在服务器上,一次在客户端上。在 nuxt.config.js 上,确保你做这样的事情

    serverMiddleware : [
        { 
          path : '/api',
          handler : '~/api/serversidefirebase.js'
        }
    ],
    plugins: [
        {
          src : '~/plugins/clientsidefirebase.js',
          ssr : false
        },
    ]
    

    在 serverMiddleware 中注册您的 API,对于所有客户端插件,请指定 ssr: false。太棒了。。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-31
      • 2021-11-05
      • 2019-01-19
      • 2017-07-03
      • 2017-09-23
      • 2019-03-14
      • 1970-01-01
      • 2018-12-27
      相关资源
      最近更新 更多