javascript - express router.get()函數與mongoose連用錯誤
世界只因有你
世界只因有你 2017-05-16 13:32:48
0
2
629

下列程式碼中被註解部分有錯誤,如果將兩端註解符號/**/去掉就會出現render渲染錯誤。

var express = require('express');
const mongoose = require('mongoose');
const UserModel=mongoose.model('UserModel');

var router = express.Router();
const ObjectId = mongoose.Schema.Types.ObjectId;
const connection = mongoose.connect('mongodb://localhost/likeread').connection;
connection.on('error',(err)=>{
        console.log(err);
    })
    .on('open',(res)=>{
        console.log('success!');
    });


/* GET users listing. */
router.get('/', function(req, res, next) {

    /*var user_model = new UserModel({username:'xiao',email:'email@.com'});
    user_model.save((err,res)=>{
        if(err)
            console.log(err);
        
    });

    connection.disconnect((err,res)=>{
        if(err)
            console.log(err);
    });*/
      res.render('user.pug');
});


module.exports = router;

錯誤

at EventEmitter.render (D:\node\likeread\node_modules\express\lib\application.js:580:17)
    at ServerResponse.render (D:\node\likeread\node_modules\express\lib\response.js:966:7)
    at D:\node\likeread\app.js:60:7
    at Layer.handle_error (D:\node\likeread\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (D:\node\likeread\node_modules\express\lib\router\index.js:315:13)
    at D:\node\likeread\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\node\likeread\node_modules\express\lib\router\index.js:335:12)
    at next (D:\node\likeread\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle_error (D:\node\likeread\node_modules\express\lib\router\layer.js:67:12)
    at trim_prefix (D:\node\likeread\node_modules\express\lib\router\index.js:315:13)
    at D:\node\likeread\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\node\likeread\node_modules\express\lib\router\index.js:335:12)
    at Immediate.next (D:\node\likeread\node_modules\express\lib\router\index.js:275:10)
    at Immediate.<anonymous> (D:\node\likeread\node_modules\express\lib\router\index.js:635:15)
    at runCallback (timers.js:668:20)
    at tryOnImmediate (timers.js:639:5)
世界只因有你
世界只因有你

全部回覆(2)
Peter_Zhu
connection.disconnect((err,res)=>{
    if(err)
        console.log(err);
});*/
  res.render('user.pug');

你這是什麼鬼? res.render('user.pug');放到函數外面?

曾经蜡笔没有小新

render和disconnect應該放到save完成的回呼函數內部執行

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!