Guest User

Untitled

a guest
Dec 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2.  
  3. // Schema Is ONly bluePrint
  4. var postSchema = mongoose.Schema({
  5. title: {type: String, required: true },
  6. content: {type: String, required: true},
  7. }, {timestamps: true});
  8.  
  9. module.exports = mongoose.model("Post", postSchema);
  10.  
  11. updatePost(id: string, title: string, content: string) {
  12. console.log('start posts.service->updatePost()');
  13. const post: Post = {
  14. id: id,
  15. title: title,
  16. content: content
  17. };
  18. this._http.put(`http://localhost:3000/api/posts/${id}`, post)
  19. .subscribe(res => console.log(res));
  20. }
  21.  
  22. app.put("/api/posts/:id", (req,res)=>{
  23. console.log('update api called:', req.params.id);
  24. const post = new Post({
  25. id: req.body.id,
  26. title: req.body.title,
  27. content: req.body.content
  28. });
  29.  
  30. Post.updateOne({_id: req.params.id}, post).then( result=> {
  31. console.log(result);
  32. res.json({message:"Update successful!"});
  33. });
  34. });
  35.  
  36. app.put("/api/posts/:id", (req, res)=> {
  37. Post.findOne(
  38. {_id:req.params.id},(err,post)=>{
  39. if(err){
  40. console.log('Post Not found!');
  41. res.json({message:"Error",error:err});
  42. }else{
  43. console.log('Found post:',post);
  44. post.title=req.body.title;
  45. post.content=req.body.content;
  46. post.save((err,p)=>{
  47. if(err){
  48. console.log('Save from update failed!');
  49. res.json({message:"Error",error:err});
  50. }else{
  51. res.json({message:"update success",data:p});
  52. }
  53. })
  54. }
  55. }
  56. );
  57. });
Add Comment
Please, Sign In to add comment