猫鼬:TypeError:无法读取未定义的属性“ findOne”

耕地

我正在使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

猫鼬模型:无法读取未定义的属性(读取“findOne”)

事务中的猫鼬 findOne():未捕获的类型错误:无法读取未定义的属性“$elemMatch”

类型错误:无法读取未定义的属性“findOne”(使用猫鼬)

TypeError:无法读取未定义的属性'findOne'

猫鼬findOne返回未定义

"error": "无法读取未定义的属性 'findOne'"

猫鼬中的 FindOne 返回未定义

环回4:@repository依赖项注入失败(TypeError:无法读取未定义的属性'findOne')

猫鼬TypeError:无法读取未定义的属性“ googleID”

TypeError:无法读取猫鼬未定义的属性“ find”

无法读取Node和Sequelize中未定义的属性'findOne'

无法使用Node.js和Sequelize读取未定义的属性'findOne'

猫鼬,NODE JS返回未定义,但在findOne内收集了正确的数据

MongoDb findOne 结果属性未定义

猫鼬findOne与“要么或”查询

了解猫鼬findOne()。remove()

猫鼬findOne()不是函数

猫鼬 findOne 不是函数

猫鼬 - 在 Multer 中 FindOne()

猫鼬查询FindOne与数组

猫鼬-无法读取未定义的属性“ push”

猫鼬模式无法读取未定义的属性“密码”

无法读取未定义的属性“值”-猫鼬5.6.0

无法读取未定义猫鼬的属性“ length”

angularJS +猫鼬:TypeError:无法读取未定义的属性“ model”

TypeError:无法读取节点js和猫鼬中未定义的属性'on'

Mongodb FindOne 返回具有未定义属性的对象

续集-findOne()。success()未定义

findOne返回onRendered中未定义的