π keep getting this error“参数类型'字符串|undefined'不能赋值给' string '类型的参数。类型“undefined”不能分配给类型“string”。ts(2345)string|当我试图创建一个新的条带对象时
我的STRIPE_SECRET_KEY在我的.env.local
文件中正确声明
import Stripe from 'stripe';
import { NextApiRequest, NextApiResponse } from 'next';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
// Replace with your desired Stripe API version
});
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { email } = req.body;
try {
const customer = await stripe.customers.create({
email: email,
});
res.status(200).json({
code: 'customer_created',
customer,
});
} catch (error) {
console.log(error);
res.status(400).json({
code: 'customer_creation_failed',
error: error,
});
}
};
export default handler;
2条答案
按热度按时间qnzebej01#
在尝试使用该字符串之前,请验证该字符串是否未定义。示例:
tzdcorbm2#
如果您确定环境变量将始终被设置,则可以使用非空Assert操作符(!)告诉TypeScript它绝对是
string
。