Slyknight
Slyknight

Reputation: 131

How to submit forms with QuillJs and body-parser?

I am new to Nodejs (and coding, in general). I am trying to build a small Content Management System. I want my potential end users to be able to style their contents (bold, italicize, hyper link, etc...) before submitting a post to my blog with a click of a button. Kind of kind the tools bar I am seeing on Stackoverflow as I am typing this

Here is what I got so far after reading Quill's guide:

form.ejs

<style>
#editor {
  height: 300px;
}

</style>

<!-- Include Quill stylesheet -->
<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">

<!-- Create the toolbar container -->
<div id="toolbar">
  <button class="ql-bold">Bold</button>
  <button class="ql-italic">Italic</button>
</div>

 <form action = '/blog' method = 'POST'>
  <!-- Create the editor container -->
  <input type = 'text' name = 'title' placeholder = 'title'>
  <label for="body">About me</label>
  <input name="body" type="hidden">
  <div id="editor">
     <p>I am at my wit's end. Stackoverflow is my last hope</p>
  </div>
    <button type="submit">Submit</button>

 </form>


<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.0.0/quill.js"></script>

<!-- Initialize Quill editor -->
<script>
  var editor = new Quill('#editor', {
    modules: { toolbar: '#toolbar' },
    theme: 'snow'
  });

  var form = document.querySelector('form');
  form.onsubmit = function() {
    console.log("hey")
    // Populate hidden form on submit
    var body = document.querySelector('input[name=body]');
    body.value = JSON.stringify(quill.getContents());

    console.log("Submitted", $(form).serialize(), $(form).serializeArray());

  };

</script> 

app.js file

var app             = require('express')(),
    mongoose        = require('mongoose'),
    Blog            = require('./model/blog'),
    bodyParser      = require('body-parser');

mongoose.connect("mongodb://localhost/quill_demo", {useMongoClient:true});    
app.set('view engine','ejs'); //EJS WILL BE THE DEFAULT FILE THAT DISPLAYS THE FRONT-END DATA
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true})); // retrieve text from submitted forms from the EJS files. 

//Add new blog to the database
app.post('/blog', function(req, res){
    var myPost = {
        title:req.body.title,
        body: req.body.body
    };
    Blog.create(myPost, function(err, newBlog){
        if(err){
            console.log(err);
        } else {
            console.log("New Blog has been added");
            newBlog.save(function(err, savedBlog){
                if(err){
                    console.log(err);
                }else {
                    console.log(savedBlog);
                    res.redirect('/blog');
                }

            });
        }
    });

});

I check the database after clicking on submit. The "title" field is working fine. But nothing is being added in the "body" field.

So, it looks like I am missing a few pieces. I would appreciate any help. I wouldn't mind if someone simply add the missing parts in my code and I'll learn from their steps later on.

P.S. I don't have a CS degree. Learning on my own. So, keep your responses in layman's terms as much as possible

Upvotes: 2

Views: 2253

Answers (1)

Slyknight
Slyknight

Reputation: 131

Nevermind, I figured it out!

I changed the script to this and it did the trick for me. The function is called when the form is submitted

<script>
  var editor = new Quill('#editor', {
      modules: { toolbar: "#toolbar" },
      theme: 'snow'
  });


  function formatField(){
    var editor = document.querySelector(".ql-editor").contentEditable = false;
    var clipboard = document.querySelector(".ql-clipboard").contentEditable = false;
    var bar = document.querySelector("input[type=text]").type="hidden"
    var p = document.querySelector("#editor");
    var myInput = document.querySelector("input[name=about]");
    myInput.value = p.innerHTML;
  }


</script> 

Upvotes: 2

Related Questions