We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

David • 10 years ago

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

Adrian Mejia • 9 years ago

You are welcome! Glad you liked it

Adam Carr • 9 years ago

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!

rjmil • 9 years ago

Thanks for the comment! You spared me a major headache.

Adrian Mejia • 9 years ago

Yes, that's true. I just fixed it and updated the tutorials with latest mongoose, node and express

Nathan Torquato • 9 years ago

Is there any live demo?
I've cloned the repo but it's not running locally. :/

Abhijith S • 10 years ago

Its good and its helped me a lot.Thank you.

Ahmed • 11 years ago

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!

Adrian Mejia • 9 years ago

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...

Jonas Östlund • 10 years ago

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.

Adrian Mejia • 9 years ago

Glad you liked it!

hakeem ahmad • 10 years ago

why did you commit the "node_modules" directory? i think it should be ignored by git

Adrian Mejia • 9 years ago

.gitignore was not set correctly. It's fixed now

Martin Jul Hammer • 8 years ago

Great article. Got my express js server up and running :)

Adrian Mejia • 8 years ago

I'm glad to hear it!

Ivo de Geus • 8 years ago

Thank you Adrian, this finally kickstarted me in MEAN development!

Adrian Mejia • 8 years ago

Awesome! You are welcome!

BrahBassim • 10 years ago

Thank you very much for this tuts. The best i found.....

Raul Abreu Leite • 10 years ago

I think this line is useless:

var mongoose = require('mongoose');

in routes/todos.js

Muito bom, tutorial! Obrigado!

Adrian Mejia • 9 years ago

Thanks! Removed!

Sai • 10 years ago

This was really helpful getting started with MEAN and REST!

Ayush Srivastava • 2 years ago
ayradv@gmail.com • 6 years ago

great tutorial !

Adrian Mejia • 6 years ago

Thanks!

Lemaire Lucas • 8 years ago

Great tutorial !

William Romano • 8 years ago

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!

Anony User • 8 years ago

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.

Russ • 8 years ago

I am wondering if it would be possible to give some hints on adapting this to have authentication. Great article.

Tim Owings • 8 years ago

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!

Bendik B • 8 years ago

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

Asuka Ishiyama • 9 years ago

this shit sucks...none of these work like it should and the walkthroughs are completely unclear

Adrian Mejia • 9 years ago

Sorry to hear that. Where did you get lost?

Asuka Ishiyama • 9 years ago

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

Adrian Mejia • 9 years ago

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.

Dennis M Dewey • 9 years ago

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.

sreng khorn • 9 years ago

where is part I??

Adrian Mejia • 9 years ago
An Karthik • 9 years ago

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.

Adrian Mejia • 9 years ago

You need to install it first "npm install mongoose" and the "node" and it would work

nice tut • 9 years ago

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.

JL • 10 years ago

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...

JL • 10 years ago

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.

Adrian Mejia • 9 years ago

Great!

Simon Dupon • 10 years ago

O, I got it: when making post in POSTMAN you should select 'x-www-form-urlencoded' tab.

Adrian Mejia • 9 years ago

Yes, without that doesn't work

jainam shah • 7 years ago

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... .

Simon Dupon • 10 years ago

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?

Adrian Mejia • 9 years ago

Also if you are using postman, make sure you have x-www-form-urlencoded selected