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)
})
})









share|improve this question







New contributor




Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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)
    })
    })









    share|improve this question







    New contributor




    Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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)
      })
      })









      share|improve this question







      New contributor




      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor




      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      Joshua Liang

      1




      1




      New contributor




      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Joshua Liang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Joshua Liang is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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






























          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.










           

          draft saved


          draft discarded


















          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.















           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga