Upon attempting to print the client-side received object on the console using console.log(this.data)
, I am consistently encountering the issue of getting undefined
. This prevents me from being able to access data.body
and display the data stored in the database. Interestingly enough, when I print the object server-side, the correct JSON object is retrieved.
It's worth noting that I am sending the object with an HTTP status code of 200. Curiously, when artificially triggering a Bad Request (res.status(400).json(docs);
), the json body is displayed correctly within an Error message.
In the client.ts file:
getEvents() {
return new Promise(resolve => {
this.http.get('http://www.mywebsite.com:8080/api/objects')
.pipe(map(data => { })).subscribe(data => {
this.data = data;
console.log(this.data);
});
});
}
And in the server.js file:
app.get("/api/objects", function (req, res) {
db.collection(COLLECTION).find({})
.toArray(function (err, docs) {
if (err) {
handleError(res, err.message, "Failed.");
} else {
console.log(docs);
res.status(200).json(docs);
}
});
});