Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- // Schema Is ONly bluePrint
- var postSchema = mongoose.Schema({
- title: {type: String, required: true },
- content: {type: String, required: true},
- }, {timestamps: true});
- module.exports = mongoose.model("Post", postSchema);
- updatePost(id: string, title: string, content: string) {
- console.log('start posts.service->updatePost()');
- const post: Post = {
- id: id,
- title: title,
- content: content
- };
- this._http.put(`http://localhost:3000/api/posts/${id}`, post)
- .subscribe(res => console.log(res));
- }
- app.put("/api/posts/:id", (req,res)=>{
- console.log('update api called:', req.params.id);
- const post = new Post({
- id: req.body.id,
- title: req.body.title,
- content: req.body.content
- });
- Post.updateOne({_id: req.params.id}, post).then( result=> {
- console.log(result);
- res.json({message:"Update successful!"});
- });
- });
- app.put("/api/posts/:id", (req, res)=> {
- Post.findOne(
- {_id:req.params.id},(err,post)=>{
- if(err){
- console.log('Post Not found!');
- res.json({message:"Error",error:err});
- }else{
- console.log('Found post:',post);
- post.title=req.body.title;
- post.content=req.body.content;
- post.save((err,p)=>{
- if(err){
- console.log('Save from update failed!');
- res.json({message:"Error",error:err});
- }else{
- res.json({message:"update success",data:p});
- }
- })
- }
- }
- );
- });
Add Comment
Please, Sign In to add comment