MEAN stack routing update Mongodb PUT method
up vote
-1
down vote
favorite
My Post method works and successfully creates an object in my database. The following is what is shown in my database after I used the post method once in my MEAN stack.
[
{
"_id": "5befb0570a2c4185be51899a",
"username": "a",
"gender": "Female",
"rating": 4,
"location": [
-121.88701271769105,
37.35748094112163
],
"__v": 0,
"updated_at": "2018-11-17T06:08:23.577Z",
"created_at": "2018-11-17T06:08:23.564Z"
}
]
// Provides method for saving new users in the db
app.post('/users', function(req, res) {
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err) {
if (err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
Now I am trying to get the put method to work to update the object and change it's rating to 5 in my database. I've been googling for hours and am really lost on the put method. Below was something I tried to get from a tutorial but it isn't working at all,
app.put('/users', (req, res) => {
db.bathrooms('users')
.findOneAndUpdate({
username: 'a'
}, {
$set: {
rating: req.body.rating
}
}, {
sort: {
_id: -1
},
upsert: true
}, (err, result) => {
if (err) return res.send(err)
res.send(result)
})
})
express post mean-stack put
New contributor
add a comment |
up vote
-1
down vote
favorite
My Post method works and successfully creates an object in my database. The following is what is shown in my database after I used the post method once in my MEAN stack.
[
{
"_id": "5befb0570a2c4185be51899a",
"username": "a",
"gender": "Female",
"rating": 4,
"location": [
-121.88701271769105,
37.35748094112163
],
"__v": 0,
"updated_at": "2018-11-17T06:08:23.577Z",
"created_at": "2018-11-17T06:08:23.564Z"
}
]
// Provides method for saving new users in the db
app.post('/users', function(req, res) {
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err) {
if (err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
Now I am trying to get the put method to work to update the object and change it's rating to 5 in my database. I've been googling for hours and am really lost on the put method. Below was something I tried to get from a tutorial but it isn't working at all,
app.put('/users', (req, res) => {
db.bathrooms('users')
.findOneAndUpdate({
username: 'a'
}, {
$set: {
rating: req.body.rating
}
}, {
sort: {
_id: -1
},
upsert: true
}, (err, result) => {
if (err) return res.send(err)
res.send(result)
})
})
express post mean-stack put
New contributor
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
My Post method works and successfully creates an object in my database. The following is what is shown in my database after I used the post method once in my MEAN stack.
[
{
"_id": "5befb0570a2c4185be51899a",
"username": "a",
"gender": "Female",
"rating": 4,
"location": [
-121.88701271769105,
37.35748094112163
],
"__v": 0,
"updated_at": "2018-11-17T06:08:23.577Z",
"created_at": "2018-11-17T06:08:23.564Z"
}
]
// Provides method for saving new users in the db
app.post('/users', function(req, res) {
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err) {
if (err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
Now I am trying to get the put method to work to update the object and change it's rating to 5 in my database. I've been googling for hours and am really lost on the put method. Below was something I tried to get from a tutorial but it isn't working at all,
app.put('/users', (req, res) => {
db.bathrooms('users')
.findOneAndUpdate({
username: 'a'
}, {
$set: {
rating: req.body.rating
}
}, {
sort: {
_id: -1
},
upsert: true
}, (err, result) => {
if (err) return res.send(err)
res.send(result)
})
})
express post mean-stack put
New contributor
My Post method works and successfully creates an object in my database. The following is what is shown in my database after I used the post method once in my MEAN stack.
[
{
"_id": "5befb0570a2c4185be51899a",
"username": "a",
"gender": "Female",
"rating": 4,
"location": [
-121.88701271769105,
37.35748094112163
],
"__v": 0,
"updated_at": "2018-11-17T06:08:23.577Z",
"created_at": "2018-11-17T06:08:23.564Z"
}
]
// Provides method for saving new users in the db
app.post('/users', function(req, res) {
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err) {
if (err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
Now I am trying to get the put method to work to update the object and change it's rating to 5 in my database. I've been googling for hours and am really lost on the put method. Below was something I tried to get from a tutorial but it isn't working at all,
app.put('/users', (req, res) => {
db.bathrooms('users')
.findOneAndUpdate({
username: 'a'
}, {
$set: {
rating: req.body.rating
}
}, {
sort: {
_id: -1
},
upsert: true
}, (err, result) => {
if (err) return res.send(err)
res.send(result)
})
})
express post mean-stack put
express post mean-stack put
New contributor
New contributor
New contributor
asked yesterday
Joshua Liang
1
1
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Joshua Liang is a new contributor. Be nice, and check out our Code of Conduct.
Joshua Liang is a new contributor. Be nice, and check out our Code of Conduct.
Joshua Liang is a new contributor. Be nice, and check out our Code of Conduct.
Joshua Liang is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349283%2fmean-stack-routing-update-mongodb-put-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown