js':'预期节点js
在尝试使用nodejs和mongoose注册用户时,我在[js] ':' Expected
的dot notation
上收到User.findOne(email: res.body.email)
错误。我试过这个
User: User.findOne(...)
但是当从邮递员发送帖子请求时,它会在运行时引发以下错误
(node:13952) UnhandledPromiseRejectionWarning: ReferenceError: body is not defined
at User.User.findOne.then.user (C:\Users\Aman\Desktop\qwerty\routes\api\users.js:14:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
(node:13952) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing ins
ide of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 1)
这是我的代码
const User = require("../../Models/User");
router.post("/register", (req, res) => ({
User.findOne({ email: req.body.email }).then(user => {
if (user) {
show email registered before message
} else {
do something
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar: req.body.avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
newUser.password = hash;
newUser
.save()
});
});
}
})
}));
回答如下:
删除函数(req, res) =>
体外的括号。它应该如下所示:
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email })
// other code inside
});
() => ({})
将期望返回一个对象文字表达式,例如JSON对象。 () => {}
将执行函数体内的语句。
在MDCN阅读更多内容:https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Syntax
js':'预期节点js
在尝试使用nodejs和mongoose注册用户时,我在[js] ':' Expected
的dot notation
上收到User.findOne(email: res.body.email)
错误。我试过这个
User: User.findOne(...)
但是当从邮递员发送帖子请求时,它会在运行时引发以下错误
(node:13952) UnhandledPromiseRejectionWarning: ReferenceError: body is not defined
at User.User.findOne.then.user (C:\Users\Aman\Desktop\qwerty\routes\api\users.js:14:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
(node:13952) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing ins
ide of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 1)
这是我的代码
const User = require("../../Models/User");
router.post("/register", (req, res) => ({
User.findOne({ email: req.body.email }).then(user => {
if (user) {
show email registered before message
} else {
do something
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar: req.body.avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
newUser.password = hash;
newUser
.save()
});
});
}
})
}));
回答如下:
删除函数(req, res) =>
体外的括号。它应该如下所示:
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email })
// other code inside
});
() => ({})
将期望返回一个对象文字表达式,例如JSON对象。 () => {}
将执行函数体内的语句。
在MDCN阅读更多内容:https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Syntax