无法初始化NodeJS GC函数



尽管运行良好的VS代码,但到目前为止,我的第一个NodeJS云函数没有成功。正在获取以下错误

Function cannot be initialized. Error: function terminated.

通过查看日志,我看到了一些潜在的问题

Detailed stack trace: ReferenceError: supabase_public_url is not defined
Provided module can't be loaded (doesn't specify)

思考:我是不是对秘密经理做错了,使用酒吧/酒吧不正确?

我的代码index.js

import { createClient } from '@supabase/supabase-js'
import sgMail from "@sendgrid/mail"
import { SecretManagerServiceClient } from '@google-cloud/secret-manager'
//activate cloud secret manager 
const client = new SecretManagerServiceClient()
const supabaseUrl = client.accessSecretVersion(supabase_public_url)
const supabaseKey = client.accessSecretVersion(supabase_service_key)
const sendgridKey = client.accessSecretVersion(sendgrid_service_key)

sgMail.setApiKey(sendgridKey)
const supabase = createClient(supabaseUrl, supabaseKey)
// get data for supabase where notifications coins are true
const supabaseNotifications = async() => {
let { data, error } = await supabase
.from('xxx')
.select('*, xxx!inner(coin, xx, combo_change, combo_signal, combo_prev_signal), xxx!inner(email)')
.eq('crypto_signals.combo_change', true)
if(error) {
console.error(error)
return
}
return data
}
//create an array of user emails from supabase data
const userEmail = (data) => {
try {
const emailList = []
for (let i of data) {
if (emailList.includes(i.profiles.email) != true) {
emailList.push(i.profiles.email)
} else {}
}
return emailList
}
catch(e) {
console.log(e)
}
}
// function to take email list and supabase data to generate emails to users
const sendEmail = (e, data ) => {
try {
for (let i of e) {
const signalList = []
for (let x of data) {  
if(i == x.profiles.email) {
signalList.push(x)
} else {}
}

// create msg and send from my email to the user 
const msg = {
to: i,
from:"xxxx",
subject: "Coin notification alert from CryptoOwl",
text: "One or more of you coins have a new signal",
html: signalList.toString()
}
sgMail.send(msg)
console.log(i)
}
}
catch(e) {
console.log(e)
}
}
// main function combines all 3 functions (supabase is await)
async function main(){
let supabaseData = await supabaseNotifications();
let supabaseEmails  = userEmail(supabaseData);
let sendgridEmails = sendEmail(supabaseEmails, supabaseData);
}

exports.sendgridNotifications = (event, context) => {
main()
};

我的包.json与类型模块使用导入以上

{
"type":"module",
"dependencies":{
"@sendgrid/mail":"^7.6.1",
"@supabase/supabase-js":"1.30.0",
"@google-cloud/secret-manager": "^3.11.0"
}
}

我一点也不熟悉Google Secret Manager,但快速查看Node.js库文档可以发现(如果我没有弄错的话(accessSecretVersion()是一种异步方法。

事实上,我们在文档中发现了如下示例:

async function accessSecretVersion() {
const [version] = await client.accessSecretVersion({
name: name,
});
// Extract the payload as a string.
const payload = version.payload.data.toString();
// WARNING: Do not print the secret in a production environment - this
// snippet is showing how to access the secret material.
console.info(`Payload: ${payload}`);
}

请参阅https://cloud.google.com/secret-manager/docs/samples/secretmanager-access-secret-version#secretmanager_access_secret_version-nodejs

相关内容

  • 没有找到相关文章

最新更新