Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

node.js - Typescript node express router and second argument. Typescript-eslint/no-misused-promises

What should I do with function of registration to fix @typescript-eslint/no-misused-promises. The one way I found how to fix this problem is write by eslint.

"@typescript-eslint/no-misused-promises": [
  "error",
  {
    "checksVoidReturn": false
  }
]

But I'd like to solve this problem as so as eslint requires. Any ideas?

import PromiseRouter from "express-promise-router";
import { login, registration } from './authController';

const router = PromiseRouter();


> ESLint: Promise returned in function argument where a void return was
> expected.(@typescript-eslint/no-misused-promises)

router.post('/registration', registration);

Here is full function of registration

export async function registration(req: TreqBodyReg, res: Response): Promise<void> {
  try {
    const { email, password } = req.body;
    const candidate = await ModelUser.findOne({ email }) as TuserFromDB;
    if (candidate) {
      res.status(400).json({ message: `There is user with email ${email}` });
      return;
    }
    const hashPassword = bcrypt.hashSync(password, 7);
    const user = new ModelUser({ email, password: hashPassword });
    await user.save();
    res.status(200).json({ message: `The user by email ${email} was successfully registered` });
  } catch (err: unknown) {
    res.status(400).json({ message: err });
  }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...