UnhandledPromiseRejectionWarning: TypeError: (0 , _referralService.default) is not a function

时间:2020-12-26 11:29:43

标签: javascript node.js mongodb mongoose ejs

我正在尝试使用 node 和 ejs 创建一个推荐系统,到目前为止链接有效,新用户可以使用它进行注册,但是当我尝试保存注册到推荐人的新用户时出现问题,但我一直得到一个错误,我厌倦了在网上搜索类似的问题但没有找到,下面是我的模型和路线。

用户模型

const userSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    email: {
        type: String,
        trim: true,
        required: true,
        unique: true,
        lowercase: true
    },
    emailToken: String,
    isVerified: Boolean,
    username: String,
    password: String,
    isAdmin: Boolean,
    refId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "referral",
    },
    plan: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "plan",
        }
    ]


})

userSchema.plugin(passportLocalMongoose);

const User = mongoose.model("User", userSchema);

export default User;

推荐模型

import mongoose, { mongo } from 'mongoose';

const referralSchema = new mongoose.Schema({
    referralId: {
        type: String,
        unique: true
    },
    referralLink: {
        type: String,
        unique: true
    },
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    createdAt: {
        type: Date,
        default: Date.now()
    }

})

const Referral = mongoose.model("Referral", referralSchema);

export default Referral;

服务

import Referral from '../models/referral';


export async function checkReferrer(query) {
  try {
    const referral = await Referral.findOne(query).populate({
      path: "userId",
    })
    //If referral is not found, throw error.
    if (!referral) {
      throw new Error("Invalid Referral")
    }
    return referral
  } catch (err) {
    throw new Error(err)

    console.log(err)
  }
}

路线

import checkReferrer from '../services/referralService';

router.post("/register", async (req, res) => {
    const newUser = new User({
        username: req.body.username,
        firstName: req.body.firstname,
        lastName: req.body.lastname,
        email: req.body.email,
        emailToken: crypto.randomBytes(64).toString('hex'),
        isVerified: false,
        isAdmin: false,
    });
    //Checks if register link contains query "reflink"
    if (req.query.reflink > "") {
        if (err) {
            console.log(err)
        }

        //Validate referral link and gets the referrer
        const referral = await checkReferer({
            referralId: req.query.reflink,
        })
        console.log(referral)
        // Saves the refId as the person that referred
        newUser.refId = referral

        console.log(newUser);

        User.register(newUser, req.body.password, async (err, user) => {
            if (err) {
                req.flash("error", err.message);
                console.log(err)
                return res.render("register");
            }

我保留错误

<块引用>

(node:13872) UnhandledPromiseRejectionWarning:
类型错误:referralService.checkReferrer 不是函数

有人可以指出我做错了什么吗?

0 个答案:

没有答案