Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 541 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the return of a created object in Mongoose with express api

#1
In my express api, using mongoose, i create a new user on api using User.create. How i can get the return of the created object to include on the response?

My method to create a object is:

async store (req, res) {
const { name, lastName, email, password } = req.body

if (await User.findOne({ email })) {
return res.status(400).json({ error: 'User already exists' })
}

try {
await User.create({
name,
lastName,
email,
password,
role: 'USER_ROLE'
})

res.status(201).json({
ok: true,
message: 'User created'
})
} catch (e) {
res.status(500).json({
ok: false,
message: 'Failed to create User'
})
}
}

I want to return the user created on the res, like:

res.status(201).json({
ok: true,
message: 'User created',
user: user

})
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through