How to debug Mongoose and MongoDB in a MEAN stack? - node.js

I'm learning the MEAN stack and I'm using npm start to run my application. I have some issues with Mongoose and MongoDB but all I get is POST /page 500 63.925 ms - 961
This is the directory structure of my app, based on this tutorial:
HTML views/
Angular.js public/javascript/
Express.js routes/
Node.js app.js
Mongoose js models/, connected in app.js
Mongo db connected in app.js
What can I do to get helpful Mongoose and MongDB debug messages?

Using node-inspector is very helpful. This question and answer showed me how to set it up in my case.

Related

Router on NodeJS express

I'm creating a nodeJS application and i was starting my app with nodemon app.js, everything was working well but when i stopped to restart it was impossible to restart. I found nothing about that on internet.
For informations i followed the node postgres documentation for the project structure using async/await : https://node-postgres.com/guides/async-express
I'm stucked in that problem and can't understand why it happens, I just want to restart my projet
Thanks for your help and have a nice day !
error at launch
I replaced the following requirement: const Router = require('express-promise-router'); to: const Router = require('router');

How to fix nodemon app crash after added mongoose

I am setting up an environment with mongodb, node.js, express.js and mongoose. mongoDB is in its own container, and the same with the rest.
It all worked fine until I tried to add mongoose.
After this line was added
const mongoose = require("mongoose");
I got an error-message when a ran docker-compose up
Error-message
This is my package.json
Package.json
This is my dockerfile for the api
Dockerfile
Does anyone knwo how to fix this?

How to connect Mongo DB in a webpack dev server

I would like to connect to Mongo DB using a webpack dev server. While the connection using the node mongodb driver and configuring in server.js is direct and straight forward, I am thinking of a way to do the same using webpack dev server in development (mainly for the hot loading advantage).
I understand that there is a way of achieving the same using a webpack middleware, but is there another easier and better way of doing it.
webpack dev-server is generally a simple Express or similar node.js server. It's essentially exactly the same as writing is on an express.js server.
npm install mongoose mongodb --save-dev
const mongoose = require('mongoose'); // Replace with import as desired
const mongoConnectString = 'mongodb://localhost/database-name-here';
mongoose.connect(mongoConnectString, (err) => {
if (err) {
console.log('Err, could not connect to the database.');
}
});
Replace the mongoConnectString as needed for developing or using databases that aren't local to your machine.

Grunt and express.js server

Currenty I'm using grunt with karma and jasmine to run my tests etc. for my Angular app.
I want to connect this app to a mongo database and was wondering what the best way to do this is. Should I keep using grunt and just connect to a database and use it all the way, or should I use an Express server as my main server connected to the database and run the tests with grunt?
Initially I want to publish this project to heroku, and I know you can do this by just adding a static server.js (wich I do not currently have) like this.
var express = require('express');
var port = process.env.PORT || 3000;
var app = express();
app.use(express.static(__dirname + ‘/public’));
app.listen(port);
and modify the gruntfile.js with this:
tasks
grunt.registerTask('heroku',
['compass:dist', 'autoprefixer', 'imagemin']);
What is the best way to do this?
I see, I feel you have slight misconception of what grunt is. Grunt is a task runner. It will run other commands when for each task. Say for example if you can compile css or minifiy js or combine images before starting server you can do it with grunt. But that does not mean grunt can do all those by itself. It will be using other libraries for those.
If you are using grunt to do testing you internally using jasmine or karma js or something else. Same when you say grunt serve you use express internally start server. So grunt does not connect to mongodb. It is express which connects to mongodb. You can write grunt tasks which will start mongodb and start express server but grunt can not do either by its own.
Should you use grunt? Yes of course.

Socket.io client: Getting error "io is not defined" or "#<Object> has no method 'connect'"

Ok so I'm basically having the same problem as this. But the answers given there don't work for me. So let me re-explain my problem. First of all, here's my code:
Server-Side Javascript (app.js)
var io = require('socket.io');
...
var sio = io.listen(app);
Client-Side Javascript (client.js)
3: var socket = (s)io.connect('http://localhost:3000');
//the (s) represents testing with and without the s out of desperateness :)
Client-Side Template (Jade)
script(src='/socket.io/socket.io.js')
script(src='/javascripts/client.js')
So from what I've read it seems like socket.io should be handling putting the socket.io.js file there but I'm guessing that I have something configured wrong because it's not doing that. Anyways the errors I get with this are:
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined - client.js line 3
After doing some research it seemed that I could change the jade file to link directly to a stable file. So by changing my code to this:
Client-Side Template (Jade)
script(src='http://cdn.socket.io/stable/socket.io.js')
script(src='/javascripts/client.js')
Then the error that I get from this is:
Uncaught TypeError: Object #<Object> has no method 'connect' - client.js line 3
I've been trying to figure this out for hours now. And it seems that I need to have socket.io-client so I made sure that it is installed. I dunno if this will but I am using Express.js as well and I will give you the layout of my files.
/project
app.js /node_modules package.json /public /routes /views
/project/node_modules
/connect /express /jade /jquery /socket.io /stylus
/project/node_modules/socket.io
/benchmarks index.js Makefile package.json restrict_jsonp.patch
History.md /lib /node_modules Readme.md
/project/node_modules/socket.io/node_modules
/policyfile /redis /socket.io-client
/project/public
/images /javascripts /jquery /stylesheets
/project/public/javascripts
client.js
Anyways, any help would be greatly appreciated! :)
As Felix Loether pointed out, the API for Express changed from 2.* to 3.* After spending way too many hours trying to figure out the best way to re-work my code I decided to re-install express to an earlier version by doing this:
npm install express#2.5.8 -g
There is a lot more support (as of today) for Express 2.* so as I am still learning it's better for me to use the earlier version.

Resources