我希望该函数获取集合中新创建的文档详细信息(“订单”),并使用特定令牌将详细信息发送到我的管理 apk。
出现错误 9:1 错误 该行的长度为 173。允许的最大长度为 80 max-len
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
export const onOrderCreate = functions.firestore
.document("orders/{orderID}")
.onCreate((change) => {
const deviceTokens =
"exiZaKIsSvesZ9U8_vDkXP:................-gVbwWRBJD8x8x53pc2wHrtiFYHknJs-4ixTazX_n3";
const after = change.data();
const payload = {
data: {
title: "New Notification",
body: after.title,
},
token: deviceTokens,
};
return admin
.messaging()
.send(payload)
.catch((error) => {
console.log("FCM Failed: ", error);
});
});
这是我尝试过的,这是我面临的错误
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint --ext .js,.ts .
E:\My Office\01 Flock Foundation\13 Flock Ministries Android Apk\Development\Flock-App-V2\Modules\Flock Ministries FCM\functions\src\index.ts
9:1 error This line has a length of 173. Maximum allowed is 80 max-len
✖ 1 problem (1 error, 0 warnings)
node:events:497
throw er; // Unhandled 'error' event
^
Error: spawn npm --prefix "%RESOURCE_DIR%" run lint ENOENT
at notFoundError (C:\Users\name\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\name\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
at cp.emit (C:\Users\name\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:25)
at ChildProcess._handle.onexit (node:internal/child_process:294:12)
Emitted 'error' event on ChildProcess instance at:
at cp.emit (C:\Users\name\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:30:37)
at ChildProcess._handle.onexit (node:internal/child_process:294:12) {
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn npm --prefix "%RESOURCE_DIR%" run lint',
path: 'npm --prefix "%RESOURCE_DIR%" run lint',
spawnargs: []
}
Node.js v21.4.0
Error: functions predeploy error: Command terminated with non-zero exit code 1
试试这个
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendOrderDetailsToAdmin = functions.firestore
.document('orders/{orderId}')
.onCreate(async (snapshot, context) => {
const orderData = await snapshot.data();
const adminToken = 'YOUR_ADMIN_APK_TOKEN';//<--pass yours here
// add all the necessary info as your req to message payload
const message = {
data: {
orderId: orderData.orderId,
customerName: orderData.customerName,//add if any thing else is needed
},
token: adminToken,
};
// push the message and catch any error if encountered
return await admin.messaging().send(message)
.then((response) => {
console.log('push FCM sent successfully:', response);
})
.catch((error) => {
console.error('Error encountered while sending message:', error);
});
});