I think this is related to how I've defined my schemas, but I can't seem to find where the bug is... I have an almost identical file set up that's working perfectly and I've unfortunately not been able to find a duplicate of this issue anywhere.
When sending an API request to my local Express instance via Postman, only the 'title' request body value is stored in the database. I am sending the following simple request to my route as Application/Json (thought the same happens when using x-www-form-urlencoded):
{
"postTitle": "title goes here",
"postContent": "body goes here",
"isPublished": true
}
This is clearly being registered in express, as if I log the object I can see this data (plus timestamps and an _id):
{ _id: 5b07d9c0b8124e0599079c04,
postTitle: 'title goes here',
postContent: 'body goes here',
isPublished: true,
createdAt: 2018-05-25T09:39:12.869Z,
updatedAt: 2018-05-25T09:39:12.869Z,
__v: 0 }
However, when I send a get request to my route on this object using its ID, I receive the following in response:
{ "_id": "5b07d9c0b8124e0599079c04" }
Likewise, if I send a request to list all objects, I receive the following response:
{
"posts": [
{
"_id": "5b07d9c0b8124e0599079c04"
},
{
"_id": "5b07d9c0b8124e0599079c03"
},
{
"_id": "5b07d9914f10ce058f137eba"
}
]
}
Weirdly, sometimes the post title sent as part of the response is included in the response, and sometimes it isn't.
My schema is as follows:
var postSchema = new Schema({
postTitle: String,
postContent: String,
isPublished: Boolean
},
{
timestamps: true
});
My post API route for POST requests is as follows:
router.post('/posts', (req, res, next) => {
var postTitle = req.body.postTitle;
var postContent = req.body.postContent;
var isPublished = req.body.isPublished;
var newPost = new Post({
postTitle: postTitle,
postContent: postContent,
isPublished: isPublished
});
newPost.save(function (error) {
if (error) {
console.log(error)
}
res.send({
success: true,
message: 'Post saved successfully!'
})
})
});
(If you're not using Router, you'll have 'app.post' instead of 'router.post') Again, this is a bit longwinded but everything works fine.
My GET route is as follows:
router.get('/posts', (req, res) => {
Post.find({}, 'title content published', function (error, posts) {
if (error) { console.error(error); }
res.send({
posts: posts
})
}).sort({_id:-1})
});
OK - so, by going through my code in detail I've figured out where I was going wrong and fixed the issue, however, in my searching I found very little in the way of results. I'm pretty new to Express, so I'm going to outline the cause of the issue and how I resolved it in order to potentially save someone else a bunch of time if they make the same stupid mistake.
Now, the issue I'm having results from the way I was retrieving the data and serving that in response to get requests. As an example, here's my GET route to list all of the objects.
I was entirely focusing on the post request and assuming it was a problem with the database. It turns out what I'd actually done, is in order to make my schemas and routes less confusing, I'd changed the names of the relevant variables. What I'd forgotten to do, however, is update this line in my GET route to reflect the change:
Post.find({}, 'postTitle postContent isPublished', function (error, posts) {
Which I'd left as:
Post.find({}, 'title content published', function (error, posts) {
The reason the title sometimes displayed is that I tried undoing changes back and forth to spot the issue.
I know this is a super basic query but I got stuck on this for the best part of a day, and the only other relevant discussion on this ended with OP saying that it magically fixed itself.
Related
I recently encounter a problem that is regarding to the CRUD development process. When I update a property and send the response to the postman, postman always display the previous data. For example, after I clicked send on Postman, it displays "weeks" : "11" instead of "10". ==>First Click
Then when I click the send button the second times, it updates the postman display "week" : "10" instead. ===> Second Click
Have anyone encounter this problem before? What is the best solution?
Here is the link to the code related to my questions: https://codesandbox.io/s/pensive-tdd-c9rej
I just don't know if this only happens to postman or it does happen to all the final production as well.
I will answer you real quick. You just need to add some options to the query.
You should set the new option to true to return the document after update was applied.
https://mongoosejs.com/docs/tutorials/findoneandupdate.html#:~:text=As%20the%20name%20implies%2C%20findOneAndUpdate,was%20before%20update%20was%20applied.&text=You%20should%20set%20the%20new,document%20after%20update%20was%20applied.
exports.unblockUser = withCatchErrAsync(async (req, res, next) => {
const { userId } = req.params;
// **blocked** is set to hidden by default, now we explicitly turn that back on
const doc = await User.findByIdAndUpdate(
userId,
{ blocked: false },
// The key Code is here
{
new: true,
runValidators: true,
}
).select("+blocked");
if (!doc) {
return next(new OperationalErr("User ID does not match any user"), 400);
}
return res.status(200).json({
status: "success",
data: {
user: doc,
},
});
});
I need a little guidance with routing in my Node/Express app. Initially, I create a new business from the Business model (works fine). After creating the business, I want a separate route which adds the current FX rates offered by that business (these fields will then be updated daily). My business model looks like this (simplified for purpose of example):
let businessSchema = new mongoose.Schema({
name: String,
category: String,
longDescription: String,
images: [ {url: String, public_id: String} ],
usdHkd: { type: String, default: "" },
hkdUsd: { type: String, default: "" },
rateCreatedAt: {
type:Date,
default:Date.now
},
});
When the business is first created, only the name, category, longDesc and images are populated, with default values for the FX rate fields. That works fine using these routes:
/* GET business new /business/new */
router.get("/new", isLoggedIn, asyncErrorHandler(businessNew));
/* POST business create /business */
router.post('/', isLoggedIn, upload.fields([{ name: 'images', maxCount: 10 }]), asyncErrorHandler(businessCreate));
I then set up separate routes/controllers like this for subsequently adding the FX rates, but I don't think these are correctly defined:
/* GET business index /business */
router.get('/:id/remittance/new', asyncErrorHandler(remittanceNew));
/* GET business index /business */
router.put('/:id/remittance', asyncErrorHandler(remittanceCreate));
//Remittances New
async remittanceNew (req, res, next) {
let business = await Business.findById(req.params.id);
res.render('remittanceViews/newRemittance', { business });
},
//Business Update
async remittanceCreate (req, res, next) {
let business = await Business.findByIdAndUpdate(req.params.id, req.body.business);
console.log(business);
//update the post with any new properties
business.usdHkd = req.body.business.usdHkd;
business.hkdUsd = req.body.business.hkdUsd;
business.rateCreatedAt = req.body.business.rateCreatedAt;
//save the updated post in the db
business.save();
//redirect to show page
res.redirect(`/business/${business.id}`);
},
The error message I get when I try to update is:
Cannot read property 'usdHkd' of undefined
Can anyone please advise where I'm going wrong here? Thanks
The error message indicates that usdHkd's parent variable in undefined. Most probably, this error is coming from business.usdHkd in business.usdHkd = req.body.business.usdHkd; (you can confirm it by adding more console.log() lines around this line and checking the outputs).
If business.usdHkd = req.body.business.usdHkd; is giving error, that means, business is undefined. However, you don't need this line as business is already updated by findByIdAndUpdate.
READ: Model.findByIdAndUpdate() and Promises in Mongoose
//Business Update
async remittanceCreate (req, res, next) {
let business = await Business.findByIdAndUpdate(req.params.id, req.body.business);
console.log(business);
// Below code is not required since findByIdAndUpdate() will update your model
/*
//update the post with any new properties
business.usdHkd = req.body.business.usdHkd;
business.hkdUsd = req.body.business.hkdUsd;
business.rateCreatedAt = req.body.business.rateCreatedAt;
//save the updated post in the db
business.save();
*/
//redirect to show page
res.redirect(`/business/${business.id}`);
},
UPDATE
You told that business is defined, but it's not getting updated. The reason is findOneAndUpdate() requires new option to be set as true else findOneAndUpdate() returns the old object (before updating it -- in a sense). So, please change the first line of remittanceCreate() to:
let business = await Business.findByIdAndUpdate(req.params.id, req.body.business, {new: true});
edit: I have realized that this is hanging at the line "res.json(comment);" of my angular-ui-route. Still not sure why.
I'm following through the tutorial here: https://thinkster.io/tutorials/mean-stack. This may be related to the fact that the tutorial is a little outdated. I've uploaded my code in it's current state to http://github.com/orieporter/flappernews
It's a simple reddit clone web app for posting links, comments, and upvoting them. I am getting a "RangeError: Maximum call stack size exceeded" error each time I add the first comment to a post. Subsequent comments work no problem. When I restart my node server and reload the page the comment that broke the server is there. Can anyone help me find the problem here?
Full error:
events.js:160
throw er; // Unhandled 'error' event
RangeError: Maximum call stack size exceeded
at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:1962:24)
at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
at cloneArray (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:362:14)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:247:12)
at cloneObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:343:13)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:260:16)
at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2009:13)
at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
at cloneObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:343:13)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:260:16)
at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2009:13)
at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
at cloneArray (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:362:14)
at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:247:12)
Relevant HTML:
<form ng-submit="addComment()" style="margin-top:30px;">
<h3>Add a new comment</h3>
<div class="form-group">
<input type="text" class="form-control" placeholder="Comment" ng-model="body"></input>
</div>
<button type="submit" class="btn btn-primary">Post</button>
</form>
Angular Controller Function:
$scope.addComment = function () {
if ($scope.body === '') { return; }
posts.addComment(post._id, {
body: $scope.body,
author: 'user'
},
$scope.post);
$scope.body = '';
};
Angular Factory Function:
o.addComment = function (id, comment, post) {
return $http.post('/posts/' + id + '/comments', comment).then(function (response) {
post.comments.push(response.data);
return response.data;
});
};
Relevant Express Route:
router.post('/posts/:post/comments', function (req, res, next) {
var comment = new Comment(req.body);
comment.post = req.post;
comment.save(function (err, comment) {
if (err) { return next(err); }
req.post.comments.push(comment);
req.post.save(function (err, post) {
if (err) { return next(err); }
res.json(comment);
});
});
});
Comment Schema:
var CommentSchema = new mongoose.Schema({
body: String,
author: String,
upvotes: {type: Number, default: 0},
post: {type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
});
Post Schema:
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
I was having the same issue and took me a bit (few fun hours!) of console log debugging, Google searching, and thinkster slack hair-pulling to try to figure out what was going on. Mind you, I'm new to all of this, so wrapping my head around everything was probably the biggest challenge.
Upon further inspection, I narrowed down the cause to the res.json(comment) line as well for the create new comment route logic (routes/index.js).
Logically, I didn't understand why the whole comment object was being pushed to the post comments array when all that is needed is the comment object id. So naturally I eventually figured why not just pass the comment object id (comment._id) and see what happens. And wallah, it worked. No more RangeError: Maximum call stack size exceeded.
So that may address the issue, but my curiously stubborn self wants to know why the heck that would matter? Why does it happen only on the first comment for the post but works fine on the others?
router.post('/posts/:post/comments', auth, function(req, res, next) {
var comment = new Comment(req.body);
comment.post = req.post._id; // <-- originally 'req.post'
comment.author = req.payload.username;
comment.save(function(err, comment){
if(err){ return next(err); }
req.post.comments.push(comment._id); // <-- originally 'comment'
req.post.save(function(err, post) {
if(err){ return next(err); }
res.json(comment);
});
});
});
I figured if mongoose was just going to need the post and comment ids and not the entire objects, then why not just save the ids. Storing the complete objects seemed to cause some sort of infinite loop I believe that causes a call stack error during the response. Perhaps because both objects reference each other? But then again, I'm not sure how this all really works so if any experts out there can shed some light, please do. But that should address the issue and hopefully help others. I should mention that this is an error in the actual Flapper News code they provide. I found that out by downloading their completed source code and running it.
It's because res.json(comment); is trying to stringify the Comment object instance you've created on var comment = new Comment(req.body);
But the "stringifying" process is hanging obviously because of the Comment Object size (which is too big and doesn't have to be stringified).
You should be replacing res.json(comment); by something like
res.json({ status: "success" });
Trying to return a list of articles specific to a sectionId in angular controller. I can get back the entire list of articles using articles.query but the passed sectionId query param gets completely ignored. I tried a to abstract it to an articles service but I'm not sure how to build services correctly yet so it threw more errors. Any help / examples of how I might achieve this, preferable as a service, would be great. I am using mean.js. Thanks in advance!
Sections controller
angular.module('sections').controller('SectionsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Sections', 'SectionArticlesList', 'Articles',
function($scope, $stateParams, $location, Authentication, Sections, SectionArticlesList, Articles) {
..........
// Find existing Section
$scope.findOne = function() {
$scope.section = Sections.get({
sectionId: $stateParams.sectionId // sections return fine
});
// problem starts here
$scope.articles = Articles.query({
section:$stateParams.sectionId // this param is ignored
});
$scope.articles.$promise.then(function(data) {
// all articles in collection returned instead of section specific
console.log('Articles: '+ JSON.stringify(data));
$scope.articles = data;
});
Articles Model
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
},
section: {
type: Schema.ObjectId,
ref: 'Section'
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
UPDATE:
Following up on the advice of Kevin B and Brad Barber below, I tried adding a factory and node server route, passing the $stateParams.sectionId to the specific factory instance. I created a new route in the articles.routes.server to make sure not to have a conflict with the standard ‘/articles/:articleId’ route. Unfortunately, anything I try still either throws errors or everything in the articles collection which is the opposite of what I want to do.
section.client.controller
// Find existing Section
$scope.findOne = function() {
$scope.section = Sections.get({
sectionId: $stateParams.sectionId
});
// fails no matter what is passed to query
$scope.articles = SectionArticlesList.query($stateParams.sectionId);
//$scope.SectionArticlesList.query($stateParams.sectionId);
//$scope.articles = SectionArticlesList.query({sectionId});
$scope.articles.$promise.then(function(data) {
// still only ever returns full list of articles
console.log('Length of articles: '+ JSON.stringify(data.length));
$scope.articles = data;
});
….
articles.client.services
angular.module('articles').factory('SectionArticlesList', ['$resource',
function($resource) {
return $resource('articles/:articleSectionId', {
articleSectionId: '#_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
articles.server.routes
// Custom route
app.route('/articles/:articleSectionId')
.get(articles.listBySectionID);
// Custom binding
app.param('articleSectionId', articles.listBySectionID);
articles.server.controller
The server side controller function never appears to get called because neither of the console.logs show up.
exports.listBySectionID = function(req, res, id) {
console.log('listBySection Called....'); // this is never printed
// have tried passing - id, sectionId and {section:id}
Article.find( {section:id} ).sort('-created').populate('user', 'displayName').exec(function(err, articles) {
if (err) {
console.log('listBySection err triggered...'); // this neither
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(articles);
}
});
};
I think I have tried everything I can think of to pass the sectionId correctly but nothing has worked yet. The latest error in the console is the 404 below.Interestingly the sectionId IS getting through but as if it is looking for one single resource such as a single blog post.
GET /sectionArticles?sectionId=555bfaf29a005c30cbfe6931 404
I don't quite understand how the default mean.js /articles route and it's corresponding list function works but duplicating a similar structure and passing an id as a param to the query to retrieve only specific results doesn't.
Would really like to understand how this should be wired up. If anyone can point out what I am doing wrong I’d appreciate it!
I'm scratching my head on this.
I'm running an expressjs site with angularjs as my front-end resource, and my problem is that I've set up my API to query my datastore with a parameterized query. When I hit the route, the parameter is not appearing in my angularjs resource query, so I end up just getting the entire data set instead of one object by id.
/api/index.js:
var _getSingleRequest = function(req, res, next)
{
models.Request
.findOne({'_id': req.body.id})
.exec(function(err, request){
if(err) return res.send(500, err);
if(!request) return res.send(404, new Error("Request not found"));
res.send(request);
});
};
...
return {
_getSingleRequest: getSingleRequest
}
/server.js
...
var api = require('./api');
app.get('/api/request/:id', api.getSingleRequest);
...
/public/js/controllers/controller.js
...
function Request($scope, $resource, $routeParams, Request)
{
$scope.request = Request.query({_id : $routeParams.id});
...
}
...
/public/js/services/services.js
services.Request = function($resource)
{
return $resource('/api/request/:id', {id:'#id'}, {'get': {method:'GET', isArray: true}});
}
console
Mongoose: requests.find({}) { fields: undefined, safe: undefined, sort: [ [ 'requestedDate', 1 ] ] }
GET /api/request?_id=51b8cc2a06859bd418000001 304 179ms
it appears that the get request is coming through properly, but in the Mongoose query there are no parameters being passed.
what simple mistake am I making, and what other information can I post to help me figure out how to get out of the infinite loop of banging my head on the wall?
In your Request function, you're passing _id:
Request.query({_id : $routeParams.id});
^^^
But in your service, you're expecting id:
$resource('/api/request/:id', {id:'#id'}
^^^
That will generate requests looking like this (which is also what your log says):
/api/request/?_id=XXX
But your backend is expecting this:
/api/request/XXX
req.body.id is for the request body, not route parameters, which come from the request URI. You want req.params.id