[英]catch error: Node.js + Google Cloud Functions
請幫助 - 無法調試此功能,使用空代理它可以工作,但是當我使用任何 socks5 代理時(例如: socks5://username:pass@1.2.3.4:1234格式) - 失敗,但我不明白哪里出了問題,怎么調試。 任何意見,將不勝感激。 謝謝 :)
包.json
"dependencies": {
"@google-cloud/firestore": "5.0.1",
"make-fetch-happen": "9.1.0"
}
index.js
const fetch = require('make-fetch-happen');
const Firestore = require("@google-cloud/firestore");
const firestore = new Firestore({
projectId: process.env.FIRESTORE_PROJECT_ID,
timestampsInSnapshots: true,
});
async function getSegments(appId, apiKey, proxy) {
const opts = {
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${apiKey}`
}
}
if (proxy) {
opts['proxy'] = proxy
}
const resp = await fetch(`https://onesignal.com/api/v1/apps/${appId}/segments`, opts)
const data = await resp.json()
return data.segments
}
exports.checkProxy = async (req, res) => {
res.set('Access-Control-Allow-Origin', '*')
if (req.method === 'OPTIONS') {
res.set('Access-Control-Allow-Methods', 'POST')
res.set('Access-Control-Allow-Headers', 'Content-Type')
res.set('Access-Control-Max-Age', '3600')
res.status(204).send('')
} else {
if (!req.body.hasOwnProperty('appIds') || !req.body.appIds || req.body.appIds.length === 0) {
res.status(403).send('appIds is required')
return
}
let resp = []
for (const appId of req.body.appIds) {
const appSnap = await firestore.collection('apps').doc(appId).get()
if (appSnap.empty) {
res.status(404).send('app not found')
return
}
const app = appSnap.data()
try {
const campaignData = await getSegments(app.appId, app.apiKey, app.proxy)
resp.push({error: false, app: app.name, appId: app.id})
} catch (e) {
resp.push({error: e, app: app.name, appId: app.id})
}
}
res.status(200).send(resp)
}
}
只需將 make-fetch-happen 模塊更新到 10.1+ 版本即可解決該問題。 感謝那個在沒有回應的情況下給我減分的人——你讓世界變得更美好,伙計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.