Firebase Cloud Functions - 意外令牌'.'



我最近擦了电脑,重新安装了所有东西。在我重置因子之前,它运行良好,但现在不允许我使用可选链接。我已经在package.json中指定了节点引擎v14。如果有人能告诉我为什么它不再工作,那就太棒了!

节点-v14.16.1

npm-v8.1.2

i  deploying functions
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
Error: Error occurred while parsing your function triggers.
/Users/ivanchau/dev/autotransportappfbcloud/functions/db/orders/onWrite.f.js:7
const _pm = order?.payment_method;
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1052:16)
at Module._compile (internal/modules/cjs/loader.js:1100:27)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:10)
at Module.load (internal/modules/cjs/loader.js:984:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1024:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/ivanchau/dev/autotransportappfbcloud/functions/index.js:49:31)

软件包.json

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"@googlemaps/google-maps-services-js": "^2.3.1",
"algoliasearch": "^4.8.4",
"camelcase": "^5.3.1",
"email-templates": "^6.0.0",
"es6-promise-pool": "^2.5.0",
"excel4node": "^1.7.2",
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.16.0",
"glob": "^7.1.4",
"lodash": "^4.17.19",
"moment": "^2.24.0",
"moment-timezone": "^0.5.25",
"nodemailer": "^6.2.1",
"pug": "^2.0.3"
},
"private": true,
"engines": {
"node": "14"
},
"devDependencies": {
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
"prettier": "^2.2.1"
}
}

eslintrc.json

{
"env": {
"es6": true,
"node": true
},
"extends": [
"plugin:prettier/recommended"
],
"plugins": ["prettier"],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"prettier/prettier": ["error", {
"endOfLine":"auto"
}]
}
}

代码示例

const { log } = require("firebase-functions/lib/logger");
const functions = require("firebase-functions");
const moment = require("moment-timezone");
const admin = require("../../firebaseConn.js");
function updateTotal(order, negative = false) {
const _pm = order?.payment_method;
const _isConfirmed = ["confirmed", "waiting"].includes(
order.status.toLowerCase()
);
const _cost = parseFloat(_isConfirmed && _pm === "Cash" ? order.cost : 0);
const _receipt = parseFloat(
_isConfirmed && _pm === "Cash" ? order.receipt : 0
);
const _no_pay_cost = parseFloat(
_isConfirmed && ["Credit Card", "不用收錢"].includes(_pm) ? order.cost : 0
);
const _offset = negative ? -1 : 1;
const _vals = {
cost: admin.firestore.FieldValue.increment(_cost * _offset),
receipt: admin.firestore.FieldValue.increment(_receipt * _offset),
no_pay_cost: admin.firestore.FieldValue.increment(_no_pay_cost * _offset),
};
const dateTime = moment(order.dateTime?.toDate()).tz("America/Toronto");
const y = dateTime.format("YYYY"),
m = dateTime.format("M"),
m0 = dateTime.format("MM"),
d = dateTime.format("DD");
const totalRef = admin.firestore().collection("orderTotal");
const monthlyTotal = {
[`${y}-${m}`]: _vals,
};
const dailyTotal = {
[`${y}-${m}`]: {
[`${y}-${m0}-${d}`]: _vals,
},
};
log(monthlyTotal);
log(dailyTotal);
const t1 = totalRef.doc("monthlyTotal").set(monthlyTotal, { merge: true });
const t2 = totalRef.doc("dailyTotal").set(dailyTotal, { merge: true });
return Promise.all([t1, t2]);
}
module.exports = functions.firestore
.document("orders/{oid}")
.onWrite((change, context) => {
const _oldOrder = change.before.data();
const _newOrder = change.after.data();
const _oid = context.params.oid;
const promises = [];
if (_oldOrder) {
const t1 = updateTotal(_oldOrder, true);
promises.push(t1);
log(`Deducted old order from monthly total! ${_oid}`);
}
if (_newOrder) {
const t2 = updateTotal(_newOrder);
promises.push(t2);
log(`Added monthly total! ${_oid}`);
}
return Promise.all(promises);
});

在一些不同的公共问题中提到了使用"可选链接"的问题。例如,在Github问题中,也有一个stackoverflow的答案。stackoverflow中提出的答案似乎是最合适的,因为Firebase实现似乎没有将表达式解释为压缩和适当扩展。

谷歌和工程师们都知道这一点,因此在Firebase函数中发布这一功能之前,最好在不链接的情况下制定表达式。

文档中写道:

可选链接不能用于未声明的根对象,但可以用于未定义的根对象。

在您的代码中,看起来像&quot_prg";没有声明?

moment(_prg?.startDate?.toDate());

相关内容

  • 没有找到相关文章

最新更新