首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法初始化NodeJS GC函数

无法初始化NodeJS GC函数
EN

Stack Overflow用户
提问于 2022-02-12 10:07:37
回答 1查看 199关注 0票数 0

尽管我的第一个NodeJS云功能运行良好,但到目前为止还是没有成功。跟踪误差

代码语言:javascript
复制
Function cannot be initialized. Error: function terminated.

纵观日志,我发现了一些潜在的问题

代码语言:javascript
复制
Detailed stack trace: ReferenceError: supabase_public_url is not defined
Provided module can't be loaded (doesn't specify)

想法:我是不是做错了秘密经理和使用酒吧/潜艇不正确?

我的代码index.js

代码语言:javascript
复制
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使用上面的导入

代码语言:javascript
复制
{
  "type":"module",
  "dependencies":{
    "@sendgrid/mail":"^7.6.1",
    "@supabase/supabase-js":"1.30.0",
    "@google-cloud/secret-manager": "^3.11.0"
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-02-12 10:38:13

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

作为一个事实,我们在文档中找到了如下例子:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71090886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档