最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

js':'预期节点js

IT培训 admin 2浏览 0评论

js':'预期节点js

在尝试使用nodejs和mongoose注册用户时,我在[js] ':' Expecteddot 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] ':' Expecteddot 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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论