如何在React中使用Admin firebase和Firebase功能从Firestore导出CSV?

时间:2018-12-13 21:15:41

标签: node.js reactjs firebase

我正在尝试从Firestore导出csv文件。 我要导出的这些数据是每个客户端输入的。

现在我正在使用firebase-functions,firebase-admin和json2csv,但是 我无法导出。

在index.js中,我导入了一些模块,例如firebase-functions和firebase-admin。我为“ http请求”定义了一个函数,用于从firestore中导出csv数据。

在app.js中,我导入index.js和我在其中定义的函数,并将该函数附加到。

因此,现在通过“ console.log()”,我知道“ button”可用,一个功能也处于活动状态,console.log('CCC')和('DDD')处于活动状态,但我认为“ https .request”未激活...

请帮助我...

下面是我的代码。

import React from 'react';
import ReactDOM from 'react-dom';
import firebase from 'firebase';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

require('firebase/firestore');

var config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: ""
  };
  firebase.initializeApp(config);

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.cert({
  projectId: '',
  clientEmail: '',
  privateKey: ''
}),
databaseURL: ''
});

export function userlist() {
  console.log('CCC')
  functions.https.onRequest((request, response) => {
    console.log('onRequest')
    const json2csv = require("json2csv").parse;
    const db = admin.firestore();
    const ordersRef = db.collection('users');
    return ordersRef.get()
     .then((querySnapshot) => {
       console.log('then')
       const orders = [];
       querySnapshot.forEach(doc => {
         const order = doc.data();
         orders.push(order);
       });
       const csv = json2csv(orders);
       response.setHeader(
         "Content-disposition",
         "attachment; filename=users.csv"
       );
       response.set("Content-Type", "text/csv");
       response.status(200).send(csv);
       return ""
     }).catch((err) => {
       console.log('catch')
       response.status(200).send("エラー発生: " + err);
       return Promise.resolve();
     });
 })
     console.log('DDD')

}


 ReactDOM.render(<App />, document.getElementById('root'));

 serviceWorker.unregister();

以下是我的警告。

./node_modules/http2/lib/protocol/index.js
 Critical dependency: require function is used in a way in which        
 cannot be statically extracted

./node_modules/firebase-functions/lib/config.js
 Critical dependency: the request of a dependency is an expression

./node_modules/firebase-functions/lib/config.js
 Critical dependency: the request of a dependency is an expression

./node_modules/fast-crc32c/loader.js
 Critical dependency: the request of a dependency is an expression

./node_modules/grpc/src/grpc_extension.js
 Critical dependency: the request of a dependency is an expression

./node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js
 Critical dependency: the request of a dependency is an expression

./node_modules/grpc/node_modules/node-pre-  
  gyp/lib/util/versioning.js
 Critical dependency: the request of a dependency is an expression

0 个答案:

没有答案