Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodemon - app crashed - waiting for file changes before starting

I just started learning Node.js. The idea of this small application is to use express and mongosse to store some users in a based cloud database (mongoDB via mlab).

I have two seperate files :

User.js (models/User.js)

var mongoose = require('mongoose');

module.exports = mongoose.model('User', {

   email : string ,
   pwd : string

});

server.js (dossier root)

var express = require('express')
var cors = require('cors')
var bparser = require('body-parser')
var mongoose = require('mongoose')

var User = require('./models/User.js')

var app = express()

app.use(cors())
app.use(bparser.json())

app.post('/register', (req,res) => {
    userData = req.body;
    var user = new User(userData);

    user.save((err, result) => {
        if(err) console.log('IL YA UNE ERREUR')
        result.sendStatus(200);
    })

})

mongoose.connect('mongodb://user:[email protected]:61755/myapp', { useMongoClient: true } , (erreur) => {
    if(!erreur) 
    console.log('Connexion etablie');
})


app.listen(3000)

When I execute : nodemon server.js, I get the error below:

D:\Bureau\MEAN\appBackend\models\User.js:5
   email : string ,
           ^

ReferenceError: string is not defined
    at Object.<anonymous> (D:\Bureau\MEAN\appBackend\models\User.js:5:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Bureau\MEAN\appBackend\server.js:6:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
[nodemon] app crashed - waiting for file changes before starting...

Do you have any idea about this error?

like image 805
MDIT Avatar asked Nov 15 '17 17:11

MDIT


Video Answer


3 Answers

Mongoose expects you to specify types using the built-in constructor functions, which are named with capital letters, e.g. String, Number, Boolean, etc.

var mongoose = require('mongoose');

module.exports = mongoose.model('User', {

   email : String ,
   pwd : String

});
like image 110
Raphael Serota Avatar answered Oct 05 '22 23:10

Raphael Serota


define

module.exports = mongoose.model('User', new mongoose.Schema({ 

   email : 'string' ,
   pwd : 'string'
   })
});

There is no string variable in the code .

like image 41
Himanshu sharma Avatar answered Oct 06 '22 01:10

Himanshu sharma


this message will be appeared always when there's any small mistake. I also faced this when module.exports type as module.export,just missed 's' only.

like image 21
Sujeewa K. Abeysinghe Avatar answered Oct 05 '22 23:10

Sujeewa K. Abeysinghe