Currently, I am immersed in the MEAN Stack tutorial provided by Thinkster.io.
At this stage, I am integrating the node.js backend with the Angularjs frontend. The functionality includes data persistence for users to add posts and upvote them.
However, an anomaly persists where an empty data post keeps appearing as shown in the link below:
Landing page of my MEAN Stack App:
https://i.stack.imgur.com/zR7ff.png
A curious element is present beneath the post "World," displaying a blank space with a "thumbs-up" icon and a comment link. When attempting to upvote this ghost post, an error message surfaces in the console:
http://localhost:3000/posts/undefined/upvote Failed to load resource: the server responded with a status of 500 (Internal Server Error)
This undefined entity is perplexing.
Upon using the curl command curl http://localhost:3000/posts
, the database output reveals an array containing objects as follows:
[{"_id":"5850ad61c2ed2798f3d353c1","title":"Hello","link":"","__v":0,"comments": [],"upvotes":0},{"_id":"5850ad69c2ed2798f3d353c2","title":"Test","link":"","__v":0,"comments":[],"upvotes":0},{"_id":"5850ad6cc2ed2798f3d353c3","title":"World","link":"","__v":0,"comments":[],"upvotes":0}]
Evidently, the mysterious object does not exist within the database. Even dropping the MongoDB database fails to rectify the issue.
In the angular code snippet below, the retrieval of all posts from the backend in the post factory's o.getAll function showcases the inconsistency:
o.getAll = function() {
// queries the '/posts' route
return $http.get('/posts').success(function(data){
// Creates a deep copy of the returned data (ensures $scope.posts in MainCtrl is updated)
angular.copy(data, o.posts);
console.log(o.posts);
});
};
The resultant console log displays an array of 4 objects, with the last one being "undefined."
The cause behind this anomaly eludes me, and I am uncertain about how to resolve it. Extensive search efforts on platforms like Stack Overflow have yielded minimal insights on this peculiar issue. Perhaps rephrasing the query might yield more accurate results?
For reference, please find the remaining sections of my code below:
newsly/models/Comments.js
var mongoose = require('mongoose');
var CommentSchema = new mongoose.Schema({
body: String,
author: String,
upvotes: {type: Number, default: 0},
post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post'}
});
mongoose.model('Comment', CommentSchema);
... (remaining code sections continue here) ...
Any assistance in unraveling this enigma would be deeply appreciated!