我正在使用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 = 'tijl.declerck@outlook.com';
// 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;
答案 0 :(得分:1)
错误可能来自您的模型index.js文件,您是否可以共享ur models / index.js文件以使这一点更加清楚,因为如果您未定义 findOne 是猫鼬函数,这意味着Stripecustome
不是猫鼬模型的实例