无法从函数来源生成表现来源:typeError:无法读取未定义的属性(reading' secret')react js node js js

发布于 2025-02-13 20:03:23 字数 2419 浏览 3 评论 0原文

我尝试替换:

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

to

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

:但是当我运行firebase仿真器时,我会遇到一个错误:

错误:无法从源加载函数定义:从函数来源:TypeError:typeError:无法读取未定义的属性(读取'secret')

我已经将其设置为以下:

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

我可以通过运行:

firebase functions:config:get

”

我在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);

没有问题的。我定义:

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

我不知道为什么它不能读取未定义的“ functions.config()。stripe.secret”的属性

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:

Error: Failed to load function definition from source: Failed to generate manifest from function source: TypeError: Cannot read properties of undefined (reading 'secret')

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

enter image description here

My code in functions/index.js is

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2025-02-20 20:03:23

如果像我一样,您在尝试本地运行您的功能时会遇到此错误,那是因为functions.config()仅在云函数运行时可用。

如果您要在部署之前试图测试您的功能,则是有关如何执行的文档的链接:在本地运行函数。具体来说,此部分引起了人们的关注:

如果您使用自定义功能配置变量,请在运行firebase服务之前在项目的功能目录中运行以下命令。

firebase functions:config:get > .runtimeconfig.json

但是,如果您使用的是Windows PowerShell,请用以下方式替换上述命令

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

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.

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 functions:config:get > .runtimeconfig.json

However, if you're using Windows PowerShell, replace the above command with:

firebase functions:config:get | ac .runtimeconfig.json
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文