等待不在猫鼬模型上工作 .findOne({email : req.body.email})
await not working on mongoose model .findOne({email : req.body.email})
我是新手,mongodb...我正在尝试使用 user.findOne({email}) 函数 returns 用户的电子邮件带有 await 关键字但节点js 抛出错误说
let user = await User.findOne({ email: req.body.email });
^^^^^
语法错误:await 仅在异步函数中有效
const express = require('express');
const User = require("../models/user");
const {body, validationResult} = require("express-validator");
const router = express.Router();
// Listening the request at assigned route
router.post('/', [
// Validation array
body("name", "Enter name").isLength({ min: 5 }),
body("email", "Enter valid email").isEmail(),
body("password", "password must not be less than 5").isLength({ min: 5 })
] ,
(req, res) => {
// Errors in a single json...
const errors = validationResult(req)
// If there error return the error json...
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
// Check if there any user with same email...
let user = await User.findOne({ email: req.body.email });
if (user) {
return res.status(400).json({ error: "A user with this email already exist..."});
}
// Create user...
// user = await User.create({
// name : req.body.name,
// email : req.body.email,
// password : req.body.password
// })
// res.json(user)
res.send("Successful request...");
})
module.exports = router
使用关键字使回调函数异步
async (req, res) => {}
您只能在异步函数中使用 await。
然后你可以写:
(req, res) => {
像
async (req, res) => {
.
另一种方法是不使用异步(但这也行),是使用回调函数:
User.findOne({ email: req.body.email }).then((user)=>{ /* Your logic here */})
总的来说,我不想混淆async/callbacks,所以我坚持只使用异步或只使用回调:)
我是新手,mongodb...我正在尝试使用 user.findOne({email}) 函数 returns 用户的电子邮件带有 await 关键字但节点js 抛出错误说
let user = await User.findOne({ email: req.body.email });
^^^^^
语法错误:await 仅在异步函数中有效
const express = require('express');
const User = require("../models/user");
const {body, validationResult} = require("express-validator");
const router = express.Router();
// Listening the request at assigned route
router.post('/', [
// Validation array
body("name", "Enter name").isLength({ min: 5 }),
body("email", "Enter valid email").isEmail(),
body("password", "password must not be less than 5").isLength({ min: 5 })
] ,
(req, res) => {
// Errors in a single json...
const errors = validationResult(req)
// If there error return the error json...
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
// Check if there any user with same email...
let user = await User.findOne({ email: req.body.email });
if (user) {
return res.status(400).json({ error: "A user with this email already exist..."});
}
// Create user...
// user = await User.create({
// name : req.body.name,
// email : req.body.email,
// password : req.body.password
// })
// res.json(user)
res.send("Successful request...");
})
module.exports = router
使用关键字使回调函数异步
async (req, res) => {}
您只能在异步函数中使用 await。
然后你可以写:
(req, res) => {
像
async (req, res) => {
.
另一种方法是不使用异步(但这也行),是使用回调函数:
User.findOne({ email: req.body.email }).then((user)=>{ /* Your logic here */})
总的来说,我不想混淆async/callbacks,所以我坚持只使用异步或只使用回调:)