【问题标题】:NodeJS GC function cannot be initializedNodeJS GC 函数无法初始化
【发布时间】:2022-05-04 23:36:45
【问题描述】:

尽管 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)

思考:我是不是对秘密管理器做错了,并且使用了 pub/sub 不正确?

我的代码 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()
};

我的 package.json 类型为模块以使用上面的导入

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

【问题讨论】:

    标签: node.js google-cloud-functions google-secret-manager


    【解决方案1】:

    我完全不熟悉 Google Secret Manager,但快速查看 Node.js 库文档表明(如果我没记错的话)accessSecretVersion() 是一种异步方法。

    事实上,我们在 doc 示例中找到如下示例:

    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

    【讨论】:

    • 您好感谢您检查错过了!让我试一试
    • 好的解决了,实际上没有使用 accessSecretVersion :D ,再次为检查它而欢呼。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多