简体   繁体   English

无法从 function 源生成清单:TypeError:无法读取未定义的属性(读取“秘密”)反应 js 节点 js

[英]Failed to generate manifest from function source: TypeError: Cannot read properties of undefined (reading 'secret') react js node js

I try to replace:我尝试替换:

const stripe =const stripe=require("stripe")('sk_test_51KMbN...');

to

const stripe = require("stripe")(functions.config().stripe.secret);

but when I run firebase emulators, I Run into an error:但是当我运行 firebase 模拟器时,我遇到了一个错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: TypeError: Cannot read properties of undefined (reading 'secret')错误:无法从源加载 function 定义:无法从 function 源生成清单:TypeError:无法读取未定义的属性(读取“秘密”)

I already set it as below:我已经将其设置如下:

firebase functions:config:set stripe.secret=< SECRET KEY>

and I can see it by runing:我可以通过运行看到它:

firebase functions:config:get

在此处输入图像描述

My code in functions/index.js is我在 functions/index.js 中的代码是

const functions = require("firebase-functions");
const express=require("express");
const cors=require("cors");
require('dotenv').config({ path: './.env' });
//API
const stripe = require("stripe")(functions.config().stripe.secret);

//App config
const app=express();
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
//middlewares
app.use(cors({origin: true}));
app.use(express.json());
async function createCheckoutSession(req, res) {
  
  const domainUrl = process.env.WEB_APP_URL;
  const { line_items, customer_email } = req.body;
  if (!line_items || !customer_email) {
    return res.status(400).json({ error: 'missing required session parameters' });
  }
  let session; 
  try {
    session = await stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      mode: 'payment',
      line_items,
      customer_email,
      success_url: `${domainUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
      cancel_url: `${domainUrl}/canceled`,
      shipping_address_collection: { allowed_countries: ['GB', 'US'] }
    }); 
    res.status(200).json({ sessionId: session.id, });
  } catch (error) {
    console.log(error);
    res.status(400).json({ error: 'an error occured, unable to create session'});
  }}
app.get('/',(req, res)=>res.send('Hello World!'));
app.post('/create-checkout-session', createCheckoutSession);
exports.api=functions.https.onRequest(app);

the code has no problem when I define:当我定义时,代码没有问题:

 const stripe =const stripe=require("stripe")('sk_test_51KMbNcJ....');

I do not know why it cannot read properties of undefined "functions.config().stripe.secret"我不知道为什么它无法读取未定义的“functions.config().stripe.secret”的属性

If, like me, you got this error while trying to run your functions locally then it's because functions.config() is only available within the Cloud Functions runtime.如果像我一样,您在尝试在本地运行函数时遇到此错误,那是因为 functions.config() 仅在 Cloud Functions 运行时中可用。

If you are trying to test your functions before you deploy, here is the link to the documentation on how to do so: run functions locally.如果您在部署之前尝试测试您的功能,这里是有关如何执行此操作的文档的链接:在本地运行功能。 Specifically, this part is of interest:具体来说,这部分很有趣:

If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.如果您使用自定义函数配置变量,请在运行 firebase serve 之前在项目的函数目录中运行以下命令。

firebase functions:config:get > .runtimeconfig.json

However, if you're using Windows PowerShell, replace the above command with:但是,如果您使用的是 Windows PowerShell,请将上述命令替换为:

firebase functions:config:get | ac .runtimeconfig.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义的属性(读取“发送”)| Discord JS v14 - TypeError: Cannot read properties of undefined (reading 'send') | Discord JS v14 TypeError:无法读取未定义的属性(读取“authService”) - TypeError: Cannot read properties of undefined (reading 'authService') 类型错误:无法读取未定义的属性(读取“缓冲区”) - TypeError: Cannot read properties of undefined (reading 'buffer') Next.js:无法读取未定义的属性(读取“indexOf”) - Next.js: Cannot read properties of undefined (reading 'indexOf') 尝试从 firebase 集合中提取数据时出错 REACT:类型错误:无法读取未定义的属性(读取“地图”) - Getting error when trying to pull data from firebase collection REACT: TypeError: Cannot read properties of undefined (reading 'map') AsyncQueue 无法持久写入:TypeError:无法读取未定义的属性(读取“toString”) - AsyncQueue Failed to persist write: TypeError: Cannot read properties of undefined (reading 'toString') TypeError:无法在 it.fromString 处读取未定义的属性(读取“indexOf”) - TypeError: Cannot read properties of undefined (reading 'indexOf') at it.fromString TypeError:无法读取未定义的属性(读取“init”)|| Flutter web - TypeError: Cannot read properties of undefined (reading 'init') || Flutter web (flutter web) TypeError: Cannot read properties of undefined (reading 'auth') - (flutter web) TypeError: Cannot read properties of undefined (reading 'auth') Flutter 中有“TypeError: Cannot read properties of undefined (reading 'firestore')” - There is "TypeError: Cannot read properties of undefined (reading 'firestore')" in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM