title: sending data from Javascript to save in mongoDB through nodejs
body: <p>I am trying to parse an object from a javascript (a blog post head and body) through a node.js server and on to save it in the mongoDB.</p>
  
  <p>this is the parsing code:</p>
  
  <pre><code>function saveState( event ) {
  
      var url = '';
      var postTitle = headerField.innerHTML;
      var article = contentField.innerHTML;
      var post = {
                      title: postTitle,
                      article: article
                  };
  
      var postID = document.querySelector('.save').getAttribute('id');
      if(postID != "new"){
          url += "?id=" + postID
      }
  
      var request = new XMLHttpRequest();
  
      request.open("POST", "draft" + url, true);
      request.setRequestHeader("Content-Type", "application/json");
      request.send(post);
  }
  </code></pre>
  
  <p>this is sent to this node server handler:</p>
  
  <pre><code>app.post('/draft', routes.saveDraft);
  
  exports.saveDraft = function(req, res){
  var id = url.parse(req.url, true).query.id;
  var post = db.getPostByID(id);
  
  if(id){
    console.log('post id' + id);
    db.savePost(id, req.body.head, req.body.article);
  }
  else{
    db.newPost(req.body.head, req.body.article);
  }
  
  res.render('editDraft.hbs', post); //send the selected post
  }; 
  </code></pre>
  
  <p>and then, sent to one of these DB functions:</p>
  
  <pre><code>exports.newPost = function (postTitle, article) {
  new postCatalog({title:postTitle, 
                  _id:1, 
                  author:'temp', 
                  AuthorID:2, 
                  date:'2/3/12', 
                  inShort:article.substring(0,100), 
                  content:article ,
                  published:false
              }).save(function (err, login) {
      if (err) {
          return console.log('error');
      }
      else {
          console.log('Article saved');
      }
  });
  }
  
  exports.savePost = function (id, postTitle, article) {
  postCatalog.find({_id: id}).save(function (err, login) {
      if (err) {
          return console.log('error');
      }
      else {
          console.log('Draft saved');
      }
  });
  }
  </code></pre>
  
  <p>now, I just can't get this to work..
  I am new to node and I could really use your help!</p>
  
  <p>thanks</p>
  
  <p>EDITED:
  the parameters being sent to the DB saving functions were not written properly.
  but i'm still stuck in the same place, where the data is being sent but not saved correctly. I think there's something wrong with my getPostByID function but I can't figure it out:</p>
  
  <pre><code>exports.getPostByID =function (id) {
  var post = postCatalog.find({_id: id}, function(err, post){
      if(err) {
          return handleError(err);
      }
      else{
          if(post &gt; 0){
              post = post[0];
          }
          return post;
      }   
  });
  return post;
  }
  </code></pre>
  
  <p>I am using express (including bodyparser) and mongoose. view engine is hbs.</p>
  
  <p>thanks again.</p>