我正在使用node.js进行结算,并创建了一个新模型Stripecustomer
,用于保存stripe customer id
和该客户的email
。我从其他猫鼬模型中复制了主要代码并进行了更改。我曾希望立即开始使用它,但是当我尝试在此模型中查找文档时,出现以下错误:
⛔️ Error:
TypeError: Cannot read property 'findOne' of undefined
我已经看了半个小时,看不到我做错了什么。谁能告诉我我做错了什么地方?
workspace.controller.js:这是我尝试创建订阅的地方。Stripecustomer是未定义的,但是我不知道为什么,因为我将其导入到顶部
const stripe = require("stripe")("sk_test_dvebbZQPA4Vk8kKZaEuN32sD");
const {
Group, User, Workspace, Stripecustomer
} = require('../models');
const { sendErr } = require('../../utils');
const billing = async (req, res) => {
try {
const email = '[email protected]';
// get the payment plan
const plan = await stripe.plans.retrieve('plan_EK1uRUJLJcDS6e');
// get the stripe customer or create a new one
let customer;
const existingCustomerDoc = await Stripecustomer.findOne({ email: email });
// if we couldn't find an existing customer in our database...
if (!existingCustomerDoc[0]) {
// then we create a new customer
customer = await stripe.customers.create({
email,
source: 'src_18eYalAHEMiOZZp1l9ZTjSU0'
});
} else {
// we retrieve this customer in stripe
customer = await stripe.customers.retrieve(existingCustomerDoc.customer_id);
}
// subscribe the customer to the plan
// You now have a customer subscribed to a plan.
// Behind the scenes, Stripe creates an invoice for every billing cycle.
// The invoice outlines what the customer owes, reflects when they will be or were charged, and tracks the payment status.
// You can even add additional items to an invoice to factor in one-off charges like setup fees.
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ plan: plan.id }]
});
res.status(200).json({
message: 'payment complete',
obj: subscription
});
} catch (err) {
return sendErr(res, err);
}
};
stripecustomer.model.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const stripeCustomerSchema = new Schema({
email: {
type: String,
required: true
},
customer_id: {
type: String,
required: true
}
});
const Stripecustomer = mongoose.model('Stripecustomer', stripeCustomerSchema);
module.exports = Stripecustomer;
该错误可能是由于您的模型的index.js文件引起的,您可以共享ur models / index.js的文件来使情况更清楚吗,因为findOne是一个mongoose函数,如果您未定义,则意味着Stripecustome
它不是mongoose模型的实例
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句