mongodb TS[2339]:Property not exist on type '()=> Promise< (Document< unknown,{},IUser> & Omit< IUser & { _id:ObjectId; },never>)|空>'

x33g5p2x  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(156)

我使用以下命令定义了用户模式:

import mongoose from 'mongoose'
import { Model, Schema } from 'mongoose'
import bcrypt from 'bcrypt'

enum LoginType {
  EMAIL = 'email',
  GOOGLE = 'google',
}

interface IUser extends mongoose.Document {
  username: string
  email: string
  password: string
  loginTypes: LoginType
  comparePassword(password: string, callback: Function): Promise<Function>
}

interface IUserMethods extends Model<IUser> {
  comparePassword(password: string, callback: Function): Promise<Function>
}

type UserModel = Model<IUser, {}, IUserMethods>;

const userSchema = new Schema<IUser, UserModel, IUserMethods>({
  username: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    require: true,
  },
  loginTypes: {
    type: String,
    require: true,
    enum: Object.values(LoginType),
  },
})

/* callback = (err: Error, isMatch: any) => {} */
userSchema.methods.comparePassword = async function (
  password: string,
  callback: Function
): Promise<Function> {
  let result
  try {
    result = await bcrypt.compare(password, this.password)
    return callback(null, result)
  } catch (err) {
    return callback(err, result)
  }
}

export default mongoose.model<IUser>('User', userSchema)

字符串
并在登录路由中调用它,但typescript在foundUser.comparePassword(req.body.password, (err: Error, isMatch: any) => {})上显示错误Property 'comparePassword' does not exist on type '() => Promise<(Document<unknown, {}, IUser> & Omit<IUser & { _id: ObjectId; }, never>) | null>'.
以下是我的登录路径:

router.post('/credentials', (req, res) => {
  /* Check if user exists in database */
  const foundUser = async () => await User.findOne({ email: req.body.email })

  if (!foundUser) {
    res.status(401).send({ message: 'User does not exist' })
  }

  /* Check if password matches */
  foundUser.comparePassword(req.body.password, (err: Error, isMatch: any) => {})

  const jwt_token = jwt.sign(
    { name: req.body.name, email: req.body.email, id: req.body.id },
    JWT_SECRET,
    { expiresIn: '10000' }
  )
  res.status(200).send({
    name: req.body.name,
    email: req.body.email,
    id: req.body.id,
    jwt_token: jwt_token,
  })
})


我已经搜索它,并尝试this解决方案之前,但不工作.
同时,我发现

const a = new User({})
a.comparePassword(req.body.password, (err: Error, isMatch: any) => {})


就像mongoose文档说的那样,我可以正常工作,但是如果我在调用comparePassword之前使用findOne,就不工作了

xzv2uavs

xzv2uavs1#

在这一行:

const foundUser = async () => await User.findOne({ email: req.body.email })

字符串
您正在定义一个名为foundUser函数,调用该函数时将返回await User.findOne({ email: req.body.email })的结果。
正如所写的那样,您需要调用函数foundUser来实际查询MongoDB。类似于:

const foundUser = async () => await User.findOne({ email: req.body.email })
const user = await foundUser();


但我认为你的意图是这样的:

const foundUser = await User.findOne({ email: req.body.email })


这里的foundUser不是一个函数,它是MongoDB查询的结果。

相关问题