小编Utk*_*rsh的帖子

无法使用 Firebase Cloud Functions 创建 xlsx 文件

我遵循了excel4node 包的基本使用教程

为了运行代码,我有一个 https 函数,它将在与本地系统Excel.xlsx相同的目录中创建一个文件。index.js

然而,问题是每次我调用该函数时,Excel.xls都会创建一个零字节文件。

函数体是这样的:

const createXlxsFile = (req, res) => {
  const xl = require('excel4node');

  // Create a new instance of a Workbook class
  const workbook = new xl.Workbook();

  // Add Worksheets to the workbook
  const worksheet = workbook.addWorksheet('Sheet 1');
  const worksheet2 = workbook.addWorksheet('Sheet 2');

  // Create a reusable style
  const style = workbook.createStyle({
    font: {
      color: '#FF0800',
      size: 12
    },
    numberFormat: '$#,##0.00; ($#,##0.00); -'
  });

  // Set value of …
Run Code Online (Sandbox Code Playgroud)

node.js firebase google-cloud-functions

4
推荐指数
1
解决办法
2263
查看次数

使用 Moment.js 获取最近 X 天的日期

我想x使用 获取当前日期的最后日期Moment.js

const moment = require('moment');

let dates;
const dates = [];
const NUM_OF_DAYS = 12; // get last 12 dates.

for (let i = 0; i < NUM_OF_DAYS; i++) {
  date = moment();
  date.subtract(1, 'day').format('DD-MM-YYYY');
  dates.push(date);
}

console.log(dates);
Run Code Online (Sandbox Code Playgroud)

这段代码的结果如下:

[ moment("2018-07-09T11:40:04.663"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675"),
  moment("2018-07-09T11:40:04.675") ]
Run Code Online (Sandbox Code Playgroud)

这是相同的日期,而不是我想要的每次迭代的前一个日期。

我知道问题出在该行上,date = moment()因为它返回的是当前时间戳,而不是每一步的前一个时间戳。

我怎样才能解决这个问题?

javascript datetime momentjs

4
推荐指数
2
解决办法
4235
查看次数

如何使用批处理在Firestore中更新500多个文档?

我正在尝试使用包含500多个文档的集合中timestampFirestoreadmin时间戳更新字段。

const batch = db.batch();
const serverTimestamp = admin.firestore.FieldValue.serverTimestamp();

db
  .collection('My Collection')
  .get()
  .then((docs) => {
    serverTimestamp,
  }, {
    merge: true,
  })
  .then(() => res.send('All docs updated'))
  .catch(console.error);
Run Code Online (Sandbox Code Playgroud)

这引发一个错误

{ Error: 3 INVALID_ARGUMENT: cannot write more than 500 entities in a single call
    at Object.exports.createStatusError (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\common.js:87:15)
    at Object.onReceiveStatus (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:1188:28)
    at InterceptingListener._callNext (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:614:8)
    at callback (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:841:24)
  code: 3,
  metadata: Metadata { _internal_repr: {} },
  details: 'cannot write more than 500 entities in a single …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-admin google-cloud-firestore

3
推荐指数
4
解决办法
1543
查看次数

在 Firestore 中将 forEach 与单个文档查询一起使用?

当我知道返回的文档引用数量仅为 1 时,我应该如何为 Firestore 编写查询?

const query = firebase.firestore().collection('Users').where('mobile', '==', '<some mobile number>').limit(1);
Run Code Online (Sandbox Code Playgroud)

为了从此查询中获取文档,我使用了forEach循环。有没有办法在不使用循环的情况下获取文档及其数据?

let docId;

query.get().then((snapShot) => {
    snapShot.forEach((doc) => {
        docId = doc.id;
    });
    if(docId) {
        // doc exists
        // do something with the data...
    }
}).catch((error) => console.log(error.message));
Run Code Online (Sandbox Code Playgroud)

javascript firebase google-cloud-firestore

1
推荐指数
1
解决办法
4702
查看次数

Nodejs SyntaxError:无效或意外

在我的index.js文件中,出现此错误

C:\Users\Utkarsh\Desktop\LearningNode\notable\app\routes\index.js:1
(function (exports, require, module, __filename, __dirname) { ??c
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Utkarsh\Desktop\LearningNode\notable\server.js:11:1)
Run Code Online (Sandbox Code Playgroud)

index.js的实际内容是这样的:

const noteRoutes = require("./note_routes");

module.exports = function(app, db) {
    noteRoutes(app, db);
}
Run Code Online (Sandbox Code Playgroud)

我检查了奇怪的引号和目录名称。什么都没用。看来问题出在这一行。

const noteRoutes = require("./note_routes");
Run Code Online (Sandbox Code Playgroud)

编辑:

note_routes.js

module.exports = function(app, db) {
    app.post("./notes", (req, res) => {
        // We'll create the …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express

0
推荐指数
1
解决办法
3070
查看次数