Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose:TypeError:无法读取未定义的属性“findOne”_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongoose:TypeError:无法读取未定义的属性“findOne”

Javascript Mongoose:TypeError:无法读取未定义的属性“findOne”,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在node.js中进行计费工作,我创建了一个新的Stripecustomer模型,在这里我保存了stripe客户id和该客户的电子邮件。我从我的其他猫鼬模型中复制了主代码,并对其进行了修改。我曾希望立即开始使用它,但当我试图在该模型中查找文档时,我出现了以下错误: ⛔️ Error: TypeError: Cannot read property 'findOne' of undefined 我看了半个小时,看不出我做错了什么。谁能告诉我哪里做错了什么 workspace.control

我正在node.js中进行计费工作,我创建了一个新的Stripecustomer模型,在这里我保存了stripe客户id和该客户的电子邮件。我从我的其他猫鼬模型中复制了主代码,并对其进行了修改。我曾希望立即开始使用它,但当我试图在该模型中查找文档时,我出现了以下错误:

⛔️ Error:
 TypeError: Cannot read property 'findOne' of undefined
我看了半个小时,看不出我做错了什么。谁能告诉我哪里做错了什么

workspace.controller.js:这里是我尝试创建订阅的地方。Stripecustomer没有定义,但我不明白为什么,因为我在上面导入了它

stripecustomer.model.js


错误可能来自您的models index.js文件,您能否共享您的models/index.js文件以更清楚地说明这一点,因为findOne是一个mongoose函数,如果您未定义,则意味着Stripecustome不是mongoose模型的实例

我知道您需要../models,但Stripecustomer是在不同的文件中定义的,stripecustomer.model.js。我在您的代码中没有看到任何实际加载stripecustomer.model.js的内容。能否共享../models.js的内容?是否尝试在findOne中添加回调?猫鼬也许可以帮助你,你是对的,其他人做了后端,我不知道索引文件。谢谢
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);
  }
};
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;