We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
You are welcome! Glad you liked it
For anyone completing this tutorial using a current mongoose, I think the callback used in the Todo.update code sample is deprecated. Rather than getting (err, numAffected, raw) you now get (err, result) where result is an object with "ok", "nModified", and "n" keys. result.nModified will give you the number of rows affected. Hope that saves some time, cheers!
Thanks for the comment! You spared me a major headache.
Yes, that's true. I just fixed it and updated the tutorials with latest mongoose, node and express
Is there any live demo?
I've cloned the repo but it's not running locally. :/
Its good and its helped me a lot.Thank you.
Hi,
I think you forgot to include the step of installing mongoose. I had to 'npm install mongoose' before I was able to require('mongoose').
Great tutorial!
Thanks, I just updated the tutorial!
Nice Post!
It helped me a lot while I was making a module that generate the endpoints based on your sequelize models.
If you want to take a look, I would be honored! =]
https://www.npmjs.com/packa...
As a clojure/lisp programmer, I appreciate your bottom-up approach to teaching. Start with the smallest building blocks and then assemble them into something meaningful.
Glad you liked it!
why did you commit the "node_modules" directory? i think it should be ignored by git
.gitignore was not set correctly. It's fixed now
Great article. Got my express js server up and running :)
I'm glad to hear it!
Thank you Adrian, this finally kickstarted me in MEAN development!
Awesome! You are welcome!
Thank you very much for this tuts. The best i found.....
I think this line is useless:
var mongoose = require('mongoose');
in routes/todos.js
Muito bom, tutorial! Obrigado!
Thanks! Removed!
This was really helpful getting started with MEAN and REST!
Awesome Blog, also read: CRUD Operation with Node.js and MongoDB
great tutorial !
Thanks!
Great tutorial !
Hi,
Great tutorial, I have an issue with it tho: On safari I only get the first value on my ng repeat, (if I send a static array I made in the scope it works) any ideas?
Hi! Thank You for tutorial!
I was getting this error
MongoDB not working. “ERROR: dbpath (/data/db) does not exist.”
Here is the solution for the same problem if any one is getting the same.
in terminal type
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
monngod
it should start running...
thanks for the nice description,covers all basic steps.
I am wondering if it would be possible to give some hints on adapting this to have authentication. Great article.
This is a great tutorial even if I came to it late...couple of questions though. what is the reason you first create a project using npm init in the project directory and then create the main part of the app using express in the todo-api directory? Also, what is the reason for using node <cli> versus just editing a file...I apologize if these are stupid questions....Thanks!
Hi! Great tutorial, I have one problem I really cannot find an answer to by Google. I successfully retrieve an empty array when using curl -XGET, but when i try to XPOST, I get a validation error. I am using curl -XPOST https://myurl:8080/todos -d 'name=MasterRoutes&completed=false¬e=soon...'. I am posting the stacktrace below. I get the same error when using postman.
My Todo.js model is identical with yours.
I had to make one change to the routing, because express.Router() gave me an error when trying to call router.get, saying router was undefined. So i simply did router = express() and then router.get.
Any help greatly appreciated. Thanks!
Stacktrace:
ValidationError: Todo validation failed
at MongooseError.ValidationError (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:1486:32)
at model.Document.set (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:753:10)
at model._handleIndex (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:596:14)
at model.Document.set (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:556:24)
at model.Document (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:68:10)
at model.Model (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:47:12)
at new model (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:3250:13)
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1864:51
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/internal/parallel.js:27:9
at eachOfArrayLike (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/eachOf.js:57:9)
at exports.default (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/eachOf.js:9:5)
at _parallel (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/internal/parallel.js:26:5)
at parallelLimit (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/parallel.js:85:26)
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1882:5
at Function.create (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1852:17)
Changes
var router = express(); //.Router(); Last part removed
this shit sucks...none of these work like it should and the walkthroughs are completely unclear
Sorry to hear that. Where did you get lost?
down through step 3 with Mongoose and CRUD. Whenever i go to input the code, i keep getting errors and i cant go beyond that
Yeah, I know it can be frustrating when things are not workig. If you post here the errors you are having I could give some pointers.
Thank you so much for this well-structured tutorial on setting up an API using MongoDB and NodeJS ExpressJS.
I've been developing an Electron(atom-shell) application using AngularJS 1.x and my employers want me to connect it to an ASP.NET API that is using a MS SQL backend ( I know ....WTF?!?!?!?). Unfortunately I'm getting NTLM Authentication issues and cannot even use an API checker like POSTMAN to test/troubleshoot it. I'm going to try to sell them on using the appropriate technologies for this sort of project and Electron was not necessarily designed to work well with ASP.NET in the first place.
where is part I??
It's here: http://adrianmejia.com/blog...
Very nice and descriptive blog. Helped a lot.
Though I got stuck up at one point.
When we are trying CRUD using the Node CLI:
The following line is mentioned in the post:
"We can play with Mongoose in the console. In the todoApp type node to enter in the node CLI. Then:"
I am getting an error in the following command:
var mongoose = require('mongoose');
Cannot find module Mongoose.
Can anyone help me here.
You need to install it first "npm install mongoose" and the "node" and it would work
good job. i used this tut to build a different api and it worked. usually, even if you follow every step, something goes wrong because things were left out.
hi there. i've just attempted this on an ubuntu server but i'm not getting any results from my database. I had an existing mongo database so I decided to try to make a CRUD server using it. Please see my post here for details: http://stackoverflow.com/qu...
I've resolve the issue. Mongoose / mongodb (?) was pluralizing my collection name and therefore the queries were failing. I had to pass the third argument to the mongoose.model() method and explicitly tell it what the name of the collection is. Voila. Everything is working.
Great!
O, I got it: when making post in POSTMAN you should select 'x-www-form-urlencoded' tab.
Yes, without that doesn't work
Hi even if it is checked I cannot make a POST call . It returns a Not Found 404 error . Can you please help . I'm stuck here All files are also identical https://uploads.disquscdn.c... .
My code is identical to yours, but I do not get 'name', 'completed' and 'note' data in responses, only '_d', '__v' and 'updated_at':
[
{
"_id": "55a7ad265d8b9298144a75b0",
"__v": 0,
"updated_at": "2015-07-16T13:54:34.220Z"
},
{
"_id": "55a7ad3a5d8b9298144a75b1",
"__v": 0,
"updated_at": "2015-07-16T13:54:34.220Z"
},
]
Why?
Also if you are using postman, make sure you have x-www-form-urlencoded selected
Awesome Tutorial. I could easily make my first steps with the MEAN stack and REST!
Thank you so much Adrian!!
With best regards from Germany,
David