簡體   English   中英

Error: SyntaxError: Unexpected token C in JSON at position 33 in rule attachments while doing on google firebase

[英]Error: SyntaxError: Unexpected token C in JSON at position 33 in rule attachments while doing on google firebase

我正在使用 GCP 和 slack 研究 firebase。 雖然這種整合我得到了這個

錯誤:SyntaxError: Unexpected token C in JSON at position 33 in rule attachments

我已經嘗試了很多,但不幸的是我找不到它的解決方案。

實際錯誤僅在代碼中..

任何人都可以幫助我解決問題!

 const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { WebClient } = require("@slack/web-api"); admin.initializeApp(functions.config().firebase); const web = new WebClient(functions.config().gslack.api_token); const runtimeOpts = { timeoutSeconds: 30, memory: "256MB" }; const rules = []; admin.firestore().collection("gSlack").where("disabled", "==", false).onSnapshot(querySnapshot => { querySnapshot.docChanges().forEach(change => { const rule = { id: change.doc.id, ...change.doc.data() }; if (change.type === "added") { rules.splice(change.newIndex, 0, rule); } if (change.type === "modified") { rules.splice(change.newIndex, 1, rule); } if (change.type === "removed") { rules.splice(change.oldIndex, 1); } }); }); function evalTest(data, test) { try { $ = data; return eval(`'use strict';(${test});`); } catch (err) { console.error(err, test); return false; } } function evalMessageText(data, message) { if (message) { try { $ = data; return eval(`'use strict';\`${message}\`;`); } catch (err) { console.error(err, message); return `Error: ${err} in rule message:\n ${message}`; } } } function evalMessageAttachments(data, attachments) { if (attachments) { try { $ = data; return JSON.parse( eval(`'use strict';\`${JSON.stringify(attachments)}\`;`) ); } catch (err) { console.error(err, attachments); return [ { title: "GSLACK ERROR", text: `Error: ${err} in rule attachments` } ]; } } } function sendSlack(data, rule) { return web.chat.postMessage({ as_user: true, channel: rule.channel, text: evalMessageText(data, rule.message), attachments: evalMessageAttachments(data, rule.attachments) }); } exports.gSlack = functions.runWith(runtimeOpts).pubsub.topic("gslack").onPublish((message, context) => { const data = message.json; return Promise.all( rules.map(rule => { if (evalTest(data, rule.test)) { return sendSlack(data, rule); } return Promise.resolve(); }) ); });

以上是我的代碼1。下面是json文件的另一個代碼

 { "name": "gslack", "description": "", "engines": { "node": "10" }, "scripts": { "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions:gSlack", "logs": "firebase functions:log" }, "dependencies": { "@slack/web-api": "^5.0.1", "firebase-admin": "~9.2.0", "firebase-functions": "^3.11.0" }, "private": true }

上面的代碼沒有錯誤。 firebase 控制台上的數據應該是清晰的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM