I'm fairly new to working on MEAN stack applications and am working on a pre-existing MEAN stack application, which used to display some information on a page after pulling it from a mongoDB database and post it in tabular form. The information still exists in the database, but somewhere along the line, the code stopped showing data on the page. Now, I get the following error in my npm log: "CastError: Cast to ObjectId failed for value "" at path "_id". I'm having a very hard time figuring out what went wrong. I even tried to include ngResources as a dependency in the serviceA.js code below, but made no difference.
To clarify, this question is trying to determine why a specific piece of code isn't working. The previous question with a similar error discusses how to convert a string to an objectID. That's not the issue here. What I'm struggling with is how to get the relevant data out of MongoDB altogether. It seems like the correct query isn't being executed and I'm trying to figure out why.
Appreciate some help. Here's my code:
Html code (from file codeA.html):
<tr ng-repeat="x in allX | offset: offsetValue | limitTo: itemsPerPage track by x._id">
Corresponding controller code (from aController.js):
aController.controller('aCtrl', ['$scope','$http', '$state', 'serviceA', function($scope, $http, $state, serviceA, $rootScope) {
if ($state.current.name == 'codeA') {
$scope.reqLoadingHash = {};
var docs = ['doc_a', 'doc_b', 'doc_c'];
serviceA.get({}, function (response) {
$scope.allX = response.searchResult;
angular.forEach($scope.allX, function(val, key) {
var numDocs = 0;
docs.forEach(function(prop) {
if (val[prop]) {
numDocs++;
}
});
$scope.allX[key].numDocs = numDocs;
});
$scope.range();
});
}
}])
serviceA code:
(function() {
'use strict';
angular.module('routerApp')
.factory('serviceA', serviceA);
serviceA.$inject = ['$resource'];
function serviceA($resource) {
return $resource('/api/requestA/:ownerRequestId', {}, {
getRequest: {
method: 'GET',
url: '/api/requestA/user/:userId/listing/:listingId'
},
update: {
method: 'PUT'
},
delete: {
method: 'DELETE'
}
});
}
})();
Router code:
var Router = require("express").Router;
module.exports = function () {
var router = new Router();
router.route('/requestA/:ownerRequestId').get(function(req, res, next) {
ownerRequest.findById(req.params.ownerRequestId)
.populate('listing owner')
.exec(function(err, ownerRequest) {
if (err) {
return next(err);
}
res.json({searchResult: ownerRequest});
});
});
return router;
}