简体   繁体   English

解决我无法将Post var保存在MongoDB中,因为我的User.findOne(email)无法正常工作

[英]RESOLVED I cant save the Post var in the MongoDB because my User.findOne(email) doesnt work

I'm setting up a website backend. 我正在建立一个网站后端。 When i run the register from the post my User.findOne(email) give this error message: 当我从帖子中运行注册时,我的User.findOne(email)会显示以下错误消息:

(node:10052) UnhandledPromiseRejectionWarning: ObjectParameterError: Parameter "filter" to findOne() must be an object, got arthurcastilhos@gmail.com at new ObjectParameterError (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\mongoose\\lib\\error\\objectParameter.js:25:11) at model.Query.Query.findOne (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\mongoose\\lib\\query.js:2135:16) at Function.findOne (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\mongoose\\lib\\model.js:2132:13) at D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\src\\routes.js:40:27 at Layer.handle [as handle_request] (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\layer.js:95:5) at next (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\route.js:137:13) at Route.dispatch (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\route.js: (节点:10052)UnhandledPromiseRejectionWarning:ObjectParameterError:findOne()的参数“ filter”必须是一个对象,在新的ObjectParameterError(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ mongoose)处获取了arthurcastilhos@gmail.com。 \\ lib \\ error \\ objectParameter.js:25:11)在model.Query.Query.findOne(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ mongoose \\ lib \\ query.js:2135:16 ),位于Function.findOne(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ mongoose \\ lib \\ model.js:2132:13),位于D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ src \\ routes.js:40:27在Layer.handle [作为handle_request](D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)在下一个(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ route.js:137:13)在Route.dispatch(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ route.js: 112:3) at Layer.handle [as handle_request] (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\layer.js:95:5) at D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:281:22 at Function.process_params (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:335:12) at next (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:275:10) at Function.handle (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:174:3) at router (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:47:12) at Layer.handle [as handle_request] (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\layer.js:95:5) at trim_prefix (D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js 112:3)在Layer.handle [作为handle_request](D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)在D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:281:22 at Function.process_params(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:335:12)在Function.handle(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:275:10)( D:\\路由器上的D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:174:3)(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:47:12)位于Layer.handle [作为handle_request](D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ layer.js :95:5)位于trim_prefix(D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js :317:13) at D:\\Arquivo de Programas\\Projetos VS Code\\devbook\\backend\\node_modules\\express\\lib\\router\\index.js:284:7 (node:10052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. :317:13),位于D:\\ Arquivo de Programas \\ Projetos VS Code \\ devbook \\ backend \\ node_modules \\ express \\ lib \\ router \\ index.js:284:7(node:10052)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). 引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (rejection id: 1) (node:10052) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (拒绝ID:1)(节点:10052)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

I already change the imsomnia id's, the parameter from the _id to user and the user from the email. 我已经更改了失眠ID,将参数从_id更改为user,并将用户从电子邮件更改为。

My routes.js: 我的routes.js:

const routes = require('express').Router();
const bcrypt = require('bcryptjs');

const User = require('./models/UserModel');
const Post = require('./controllers/PostController');

routes.get('/', (req, res) => {
  return res.send('Hello, World!')
});

routes.post('/register', async (req, res) => {
  try {
    const user = await User.create(req.body);

    return res.send({ user })
  } catch {
    return res.status(400).send({ error: 'Registration failed' })
  }
});

routes.post('/auth', async (req, res) => {
  const { email, password } = req.body;

  const user = await User.findOne({ email }).select('+password');

  if (!user){
    return res.status(400).send({ error: 'User not found' });
  };

  if (!await bcrypt.compare(password, user.password)){
    return res.status(400).send({ error: 'Invalid password '});
  };

  res.send({ user });
})

routes.post('/post', async (req, res) => {
  const { text, email } = req.body //only test 

  const user = await User.findOne(email)

  if (!user){
    return res.status(400).send({ error: 'User not found' });
    }
    if (!text){
      return res.status(400).send({ error: 'Text not found' });
    }

  res.send(text)
});

module.exports = routes;

I expect the register from the post to show it after in the homepage 我希望帖子中的注册信息可以在首页显示出来

SOLUTION: 解:

turn the const user = await User.findOne(email) to const user = await User.findOne({ email: email }) const user = await User.findOne(email)const user = await User.findOne({ email: email })

From the error message it seems like the call to findOne() is getting just the email string and not an object. 从错误消息看来,对findOne()的调用似乎只是获取电子邮件字符串,而不是对象。 Why don't you use the same syntax you did in the other routes? 为什么不使用与其他路由相同的语法?

You wrote this in the /post route: 您是在/ post路由中编写的:

  const user = await User.findOne(email)

But it seems like you should write this: 但是似乎您应该这样写:

  const user = await User.findOne({ email } )

Correct? 正确? Here is the top half of the route for reference, with the proposed correction: 这是路线的上半部分,供您参考,并提出了建议的更正方法:

routes.post('/post', async (req, res) => {
  const { text, email } = req.body //only test 

  const user = await User.findOne({ email })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB User.findOne({email:email}) - MongoDB User.findOne({email:email}) User.findOne() 不是函数 - User.findOne() is not a function user.findOne() 总是返回 null - user.findOne() always returns null 尽管同一模型在另一个文件中工作,但我收到“User.findOne 不是函数”错误消息。 类似的问题答案不起作用 - I am receiving a "User.findOne is not a function" error message despite this same model working in another file. Similar question answers didn't work passport.authenticate 不是 function / User.findone 不是 function - passport.authenticate is not a function / User.findone is not a function 在SailsJs中充当User.Update的User.findOne - User.findOne acting as User.Update in SailsJs User.findOne 在对象解构字符串键中循环 - User.findOne is looping over in object destructuring string key SailsJs 0.10:User.findOne({_ id:id})为唯一主键返回null - SailsJs 0.10: User.findOne({_id: id}) returning null for unique primary key 如何正确使用 User.findOne({ userID: searchUserID } ??? (目前没有得到返回值) - How to use User.findOne({ userID: searchUserID } correctly ??? (currently not getting return value) 在发布路由时,请使用nodemailer发送电子邮件,并使用通行证将用户保存到mongodb。 - On post route send email with nodemailer and save user to mongodb with passport.authenticate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM