2016-11-12 77 views
0

我沿着Udemy教程跟隨。到目前爲止,一切都運行良好,直到我將應用程序連接到mongodb。應用程序運行正常,連接到數據庫,但是當我訪問表單頁面時,應該會顯示數據庫中的數據,應用程序崩潰並給我以下錯誤。Nodejs應用程序在訪問表單頁面時崩潰

app.js文件

var express = require('express'); 
 
var path = require('path'); 
 
var favicon = require('serve-favicon'); 
 
var logger = require('morgan'); 
 
var cookieParser = require('cookie-parser'); 
 
var bodyParser = require('body-parser'); 
 
var mongoose = require('mongoose'); 
 

 
var appRoutes = require('./routes/app'); 
 

 
var app = express(); 
 
mongoose.connect('localhost:27017/node-angular2'); 
 

 
// view engine setup 
 
app.set('views', path.join(__dirname, 'views')); 
 
app.set('view engine', 'hbs'); 
 

 
// uncomment after placing your favicon in /public 
 
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
 
app.use(logger('dev')); 
 
app.use(bodyParser.json()); 
 
app.use(bodyParser.urlencoded({extended: false})); 
 
app.use(cookieParser()); 
 
app.use(express.static(path.join(__dirname, 'public'))); 
 

 
app.use(function (req, res, next) { 
 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
 
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); 
 
    res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS'); 
 
    next(); 
 
}); 
 

 
app.use('/', appRoutes); 
 

 
// catch 404 and forward to error handler 
 
app.use(function (req, res, next) { 
 
    return res.render('index'); 
 
}); 
 

 

 
module.exports = app;

路由器/ app.js

var express = require('express'); 
 
var router = express.Router(); 
 
var User = require('../models/user'); 
 

 
router.get('/', function (req, res, next) { 
 

 
    User.findOne({}, function(err, doc) { 
 
     if (err) { 
 
      return res.send('Error!'); 
 
     } 
 
     res.render('node', {email: doc.email}); 
 
    }); 
 
}); 
 

 
router.post('/', function(req, res, next) { 
 
    var email = req.body.email; 
 
    var user = new User({ 
 
     firstName: 'Max', 
 
     lastName: 'Schwarz', 
 
     password: 'super-secret', 
 
     email: this.email 
 
    }); 
 
    user.save(); 
 
    res.redirect('/'); 
 
}); 
 

 
module.exports = router;

錯誤日誌

> node ./bin/www 

events.js:160 
     throw er; // Unhandled 'error' event 
    ^

TypeError: Cannot read property 'email' of null 
    at /Users/farooqkhan/Documents/Udemy/node-angular2/routes/app.js:11:39 
    at Query.<anonymous> (/Users/farooqkhan/Documents/Udemy/node-angular2/node_modules/mongoose/lib/model.js:3381:16) 
    at /Users/farooqkhan/Documents/Udemy/node-angular2/node_modules/kareem/index.js:259:21 
    at /Users/farooqkhan/Documents/Udemy/node-angular2/node_modules/kareem/index.js:127:16 
    at _combinedTickCallback (internal/process/next_tick.js:67:7) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

npm ERR! Darwin 16.1.0 
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start" 
npm ERR! node v6.9.1 
npm ERR! npm v3.10.8 
npm ERR! code ELIFECYCLE 
npm ERR! [email protected] start: `node ./bin/www` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node ./bin/www'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the udemy-nodejs-angular2 package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  node ./bin/www 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs udemy-nodejs-angular2 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls udemy-nodejs-angular2 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  /Users/farooqkhan/Documents/Udemy/node-angular2/npm-debug.log 

第二個例子。

然後我運行了一個mongodb IDE,我從github獲得了。它曾經工作正常,但現在當我訪問首頁時,它也給出了以下錯誤:

> node app.js 

(node:1054) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version. 
adminMongo listening on host: http://0.0.0.0:1234 
GET/302 8.496 ms - 56 
/Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/mongodb/lib/mongo_client.js:237 
      throw err 
     ^

TypeError: Cannot read property 'indexOf' of undefined 
    at module.exports (/Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/parse-mongo-url/index.js:10:10) 
    at new Database (/Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/mongojs/lib/database.js:43:20) 
    at module.exports (/Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/mongojs/index.js:5:12) 
    at /Users/farooqkhan/Documents/mongoIDE/adminMongo/routes/index.js:58:22 
    at connectCallback (/Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/mongodb/lib/mongo_client.js:315:5) 
    at /Users/farooqkhan/Documents/mongoIDE/adminMongo/node_modules/mongodb/lib/mongo_client.js:234:11 
    at _combinedTickCallback (internal/process/next_tick.js:67:7) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

npm ERR! Darwin 16.1.0 
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start" 
npm ERR! node v6.9.1 
npm ERR! npm v3.10.8 
npm ERR! code ELIFECYCLE 
npm ERR! [email protected] start: `node app.js` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node app.js'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the admin-mongo package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  node app.js 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs admin-mongo 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls admin-mongo 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  /Users/farooqkhan/Documents/mongoIDE/adminMongo/npm-debug.log 

我不明白這裏有什麼錯。一切都很好,我不知道突然發生了什麼。我甚至嘗試重新安裝MongoDB,但沒有任何幫助。

+0

你的'app.js'文件和錯誤日誌的來源只會比錯誤日誌更有用。 – aring

+0

您需要將body-parser模塊安裝爲中間件,以便'req.body'屬性正確包含您的表單數據。你必須從你遵循的例子中錯過了這一步。 – jfriend00

+0

body-parser是在主app.js文件中導入的。上面的代碼是來自routes/app.js – TheLearner

回答

0

它看起來像集合沒有數據,你可以驗證你的node-angular2模塊有用戶表,它有數據嗎?

您可以通過第三方工具(如robomongo)或通過shell使用mongo命令進行連接。

同時您可以修改您的代碼做一些錯誤處理

var express = require('express'); 
var router = express.Router(); 
var User = require('../models/user'); 

router.get('/', function (req, res, next) { 

    User.findOne({}, function(err, doc) { 
     if (err) { 
      return res.send('Error!'); 
     } 
     if(doc){ 
      res.render('node', {email: doc.email}); 
     }else{ 
      res.render('node', {email: null}); 
     } 
    }); 
}); 

router.post('/', function(req, res, next) { 
    var email = req.body.email; 
    var user = new User({ 
     firstName: 'Max', 
     lastName: 'Schwarz', 
     password: 'super-secret', 
     email: email 
    }); 
    user.save(function(err){ 
     if(err){ 
     console.log("couldn't save"); 
     } 
     res.redirect('/'); 
    }); 
}); 
module.exports = router; 

正如你可以看到我已經修改了你的帖子電話,我打電話的保存功能的回調,這意味着它只能在重定向重定向用戶被保存。

當res.json或res.redirect在沒有完成前面的操作的情況下被調用時,先前的操作被固有地取消。

+1

完美的工作。非常感謝你的幫助。我很感激。 – TheLearner

+0

不客氣。 :) –

相關問題