I encountered an issue while attempting to access the Question
field within the JSON document body stored in a MongoDB database. Upon executing the GET request, the result displayed as follows:
{
"_readableState": {
"objectMode": true,
"highWaterMark": 0,
"buffer": {
"head": null,
"tail": null,
"length": 0
},
"length": 0,
"pipes": null,
"pipesCount": 0,
"flowing": null,
"ended": false,
"endEmitted": false,
"reading": false,
"sync": true,
"needReadable": false,
"emittedReadable": false,
"readableListening": false,
"resumeScheduled": false,
"destroyed": false,
"defaultEncoding": "utf8",
"awaitDrain": 0,
"readingMore": false,
"decoder": null,
"encoding": null
},
"readable": true,
"domain": null,
"_events": {},
"_eventsCount": 0,
"_opts": {},
"_destroyed": false
}
Despite researching about JSON body parsers, integrating them did not resolve my issue. Provided below is my index.js
code snippet:
var express = require('express')
var mongojs = require('mongojs')
var bodyParser = require("body-parser");
var app = express()
var db = require('./myDB.js')
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json())
app.listen(4000, () => console.log('Hello'))
app.get('/getFlashCard', (req, res) => {
let flashID = req.body._id;
db.getFlashCard("Interview Questions", flashID, function(docs) {
console.log("Flashcard retrieved: ", docs);
res.send(docs);
});
});
Below is the content of my myDB.js
:
getFlashCard : function(colName, flashID, callback) {
let data = mongodb.collection("Interview Questions").find({
"_id" : flashID
});
callback(data);
}