<% if (updated) { %>
Blog post updated successfully - View post
<% } %>
Blog Post Entry Form

The url slug for this blog entry, the ending of the URL. ie 'http://localhost:5000/entry/the-itp-story'


Code sample - How to update a blog post

// web.js
// how to update a blog post

app.get("/update/:postId", function(request, response){
    
    // get the request blog post id
    var requestedPostID = request.params.postId;
    
    // find the requested document
    BlogPost.findById( requestedPostID, function(err, blogpost) {
        
        if (err) {
            console.log(err);
            response.send("an error occurred!");
        }
        
        if (blogpost == null ) {
            console.log('post not found');
            response.send("uh oh, can't find that post");

        } else {
            
            // prepare template data
            // blogpost data & updated (was this entry updated ?update=true)
            templateData = {
                blogpost : blogpost,
                updated : request.query.update
            };
            
            // found the blogpost
            response.render('blog_post_entry_update.html', templateData);
        }
        
    })
    
});

app.post("/update", function(request, response){
    
    // update post body should have form element called blog_post_id
    var postid = request.body.blog_post_id;

    // we are looking for the BlogPost document where _id == postid
    var condition = { _id : postid };
    
    // update these fields with new values
    var updatedData = {
        title : request.body.title,
        content : request.body.content,
        author : {
            name : request.body.name,
            email : request.body.email
        }
    };
    
    // we only want to update a single document
    var options = { multi : false };
    
    // Perform the document update
    // find the document with 'condition'
    // include data to update with 'updatedData'
    // extra options - this time we only want a single doc to update
    // after updating run the callback function - return err and numAffected
    BlogPost.update( condition, updatedData, options, function(err, numAffected){
        
        if (err) {
            console.log('Update Error Occurred');
            response.send('Update Error Occurred ' + err);

        } else {
            
            console.log("update succeeded");
            console.log(numAffected + " document(s) updated");
            
            //redirect the user to the update page - append ?update=true to URL
            response.redirect('/update/' + postid + "?update=true");
            
        }
    });
    
})