I've been troubleshooting a websocket function that interacts with MongoDB to fetch some data stored in the system using 'get'.
const User = require('mongoose');
const express = require('express');
const cal = require('../../app/models/data.server.model.js');
const d = require('../../app/routes/data.server.routes.js');
const data = require('./data.server.controller');
module.exports = function(app) {
require('express-ws')(app);
app.ws('/', function (ws, req) {
ws.on('message', function (msg) {
console.log("Message received from client: %s", msg);
if (msg == "Update" || msg == "update") {
const calendar = app.route('/api/data').get();
console.log(calendar);
ws.send("fromJSON " + JSON.stringify(calendar));
}
});
});
};
This connects to the following function:
module.exports = function(app) {
app.route('/api/data')
.get(data.list)
.post(users.requiresLogin, data.create);
That ultimately calls:
exports.list = function(req, res) {
Data.find().sort('-created').populate('creator', 'firstName lastName fullName').exec((err, datas) => {
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
});
} else {
res.status(200).json(datas);
}
});
};
The expected output is my data.
However, the variable 'calendar' only contains a JSON object with 'path' (the same as 'api/data'), 'stack' & 'methods' which are empty.
I am certain the data exists, as I can access it using AngularJS code, but I'm unsure how to retrieve it here. Any assistance would be greatly appreciated. Please let me know if any part of my explanation is unclear. Thank you in advance!