简体   繁体   中英

How to get data with mongoose from js output?

Here having a problem with mongoose... I'm working on blog website and tried to use quilljs. The problem is that i have output of the file from js file where are the configs of my quill.

 var quill = new Quill('#editor', { modules: { toolbar: [ [{ header: [1, 2, false] }], ['bold', 'italic', 'underline'], ['image', 'code-block'] ] }, theme: 'snow' }); $('#saveArticle').click(function(){ quill.root.innerHTML; //output });

and how could I connect that output to my mongoose. Before that i used tag.

 app.post("/compose", function(req, res){ const post = new Post({ content: req.body.output, }); post.save(function(err){ if (!err){ res.redirect("/"); } }); }); app.get("/blogs/:postName", function(req, res){ const requestedPostId = req.params.postId; Post.findOne({_id: requestedPostId}, function(err, post){ res.render("article", { content: post.content }); }); });

To send the data output to the server, you need to make a post HTTP(S) request to the endpoint( /compose ). The browser provides a way of doing this, however, it seems you are already using jQuery, consequently, you can make use of the jQuery.ajax() API provided by jQuery. To send the data to the /compose endpoint with ajax, your code should be something like this:

$('#saveArticle').click(function(){
  const output = quill.root.innerHTML;
  $.ajax({
    type: 'POST',
    url: '/compose',
    data: output,
    success: function (response){
      console.log(response);
      // Whatever you want to do after successful post
      // For example, alerting that the article is saved
      alert('Article saved');
    },
    error: function(err){
      // Whatever you want to do after a failed post
      console.error(err);
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM