简体   繁体   中英

Mongoose: TypeError: Cannot read property 'findOne' of undefined

I am working on billing in node.js and I created a new Model Stripecustomer where I save the stripe customer id and this customer's email . I kinda copied the main code form my other mongoose models and changed it. I had hoped to instantly start using it but when I tried to find a document in this model I got the following error:

⛔️ Error:
 TypeError: Cannot read property 'findOne' of undefined

I have looked at it for half an hour and I can't see what I did wrong. Can anyone tell me where I did something wrong?

workspace.controller.js: here is where I try to create a subscription. Stripecustomer is undefined, but I don't get why since I imported it on top

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;

错误可能来自您的模型index.js文件,您是否可以共享ur models / index.js文件以使这一点更加清楚,因为findOne是一个Stripecustome函数,如果您未定义,则表示Stripecustome不是猫鼬的实例模型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM