Saturday, 29 April 2017

how to get value from async function

After sending data to node server from the client side using Ajax, i need to save the save the data in my server,

app.post('/newcart', function(req, res) {
    var strJSON = req.param('strJSON');
    var objJSON = JSON.parse(strJSON);
    var answers = objJSON.answers;
    var correctAnswers = [2, 3, 1];
    var results = {};


    for (var i = 0; i < answers.length; i = i + 1) {
        var q = i + 1;
        if (answers[i] === null)
            results["q" + q] = "Unanswered";
        else if (answers[i] == correctAnswers[i])
            results["q" + q] = "Right";
        else
            results["q" + q] = "Wrong";
    }

    var respondWith = results;
    res.status(200);
    return res.send(respondWith);
});

I am able to resend the the result to the fronted, but not able to save the data into the database(mongoDB) in nodejs.



via user5667392

No comments:

Post a Comment