The questionController
below is suppose to handle a /question
path and return a response. I am just testing the connection to the database at present, thus returning a hard coded value.
However, the value received at client is not value of q
but the following:
[
{
"message": "[object Promise]"
}
]
What am I doing wrong?
question.js
const mongoUtils = require('../mongoUtils');
var db ;
var Question = require('../schema/question')
async function getQuestion(){
var q = null;
try {
//const db = dbClient.db("Questions");
console.log(`db in getQuestion ${db}`);
const areaCollection = await db.collection("Area");
console.log(`area collection is ${areaCollection}`);
q = Question({
description: "some description",
hints: ["hint1", "hint2"],
topic: 'area'
});
} catch(exception) {
console.log(`something bad happened in dbconnect: ${exception}`);
} finally {
console.log(`question object : ${JSON.stringify(q)}`);
}
return q
}
exports.questionController = function (req, res) {
db = mongoUtils.getDb();
console.log(`db in question is ${db}`);
console.log(`connected to database ${db.databaseName}`);
var question = getQuestion();
console.log('question page');
//var response = res.json([{"message": `${question}`}]);
var response = [{"message": `${question}`}];
console.log(`returning response: ${JSON.stringify(response)}`)
return res.json(response);
}