Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. ...
  2. <router-link :to="{ name: 'Edit', params: { id: todo._id }}">Edit</router-link>
  3. ...
  4.  
  5. <template>
  6. <div id="todo-edit-form">
  7. <div> Edit </div>
  8. title : <input :value="todo.title"> <br />
  9. content : <input :value="todo.content"> <br />
  10. <button @click="update(todo._id)" >Edit</button>
  11. </div>
  12. </template>
  13.  
  14. <script>
  15. export default {
  16. data: function () {
  17. return {
  18. todo: []
  19. }
  20. },
  21. created () {
  22. let id = this.$route.params.id
  23. this.$http.get(`/api/todos/edit/${id}`)
  24. .then((response) => {
  25. this.todo = response.data
  26. })
  27. },
  28. methods: {
  29. update (id) {
  30. this.$http.put(`/api/todos/${id}`, {
  31. todo: this.todo
  32. })
  33. .then(
  34. (response) => {
  35. // confirmed that the response contains the original title and content, not the modified content.
  36. this.$router.push('/todos')
  37. },
  38. (err) => {
  39. alert('Error')
  40. }
  41. )
  42. .catch(function (error) {
  43. alert('error')
  44. })
  45. }
  46. }
  47. }
  48. </script>
  49.  
  50. router.put('/:id', (req, res) => {
  51. Todo.findByIdAndUpdate(req.params.id, { $set: req.body }, (err, todo) => {
  52. if (err) {
  53. res.status(500).send('Something broke!');
  54. }
  55. res.json(todo);
  56. });
  57. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement