Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

this is the parsing 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);
}

this is sent to this node server handler:

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
}; 

and then, sent to one of these DB functions:

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');
    }
});
}

now, I just can't get this to work.. I am new to node and I could really use your help!

thanks

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:

exports.getPostByID =function (id) {
var post = postCatalog.find({_id: id}, function(err, post){
    if(err) {
        return handleError(err);
    }
    else{
        if(post > 0){
            post = post[0];
        }
        return post;
    }   
});
return post;
}

I am using express (including bodyparser) and mongoose. view engine is hbs.

thanks again.

share|improve this question
 
You completely misunderstood the asynchronous programming style of Node.js. Please read some tutorials and do very small steps with working examples. Your code is synchronous style which doesn't work. –  hgoebl Nov 16 at 8:44
 
thanks for the reply but I do believe this should work even though the procedure is syncronous. –  omerkarj Nov 16 at 11:28
 
find would return a cursor, but not the actual results. As @hgoebl said, you're not doing async programming. find returns immediately. –  WiredPrairie Nov 16 at 18:15
add comment

1 Answer

up vote 0 down vote accepted

You have to write it the asynchronous way, e.g. your getPostByID:

exports.getPostByID = function (id, callback) {

    postCatalog.find({_id: id}, function(err, post) {
        if (err) {
            callback(err);
        }
        else if (post && post.length > 0) {
            callback(null, post[0]);
        }
        else {
            callback(null, null); // no record found
        }
    });

};   

And this is true for your whole code. It's totally different and the way you tried it will never work under Node.js.

BTW there is a mongo-driver method findOne which is better suited in this special case but I didn't want to change your code too much.

share|improve this answer
 
thanks for the comments everyone, hitting the books! (literally...) –  omerkarj Nov 18 at 17:16
 
You're welcome! We all had to wrench our brains to get warm with the Node.js way ;-) Would be nice if you marked the answer as correct and/or helpful. –  hgoebl Nov 18 at 19:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.