javascript - express router.get()函数与mongoose连用错误
问题描述
下列代码中被注释部分存在错误,如果将两端注释符号/**/去掉就会出现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:nodelikereadnode_modulesexpresslibapplication.js:580:17) at ServerResponse.render (D:nodelikereadnode_modulesexpresslibresponse.js:966:7) at D:nodelikereadapp.js:60:7 at Layer.handle_error (D:nodelikereadnode_modulesexpresslibrouterlayer.js:71:5) at trim_prefix (D:nodelikereadnode_modulesexpresslibrouterindex.js:315:13) at D:nodelikereadnode_modulesexpresslibrouterindex.js:284:7 at Function.process_params (D:nodelikereadnode_modulesexpresslibrouterindex.js:335:12) at next (D:nodelikereadnode_modulesexpresslibrouterindex.js:275:10) at Layer.handle_error (D:nodelikereadnode_modulesexpresslibrouterlayer.js:67:12) at trim_prefix (D:nodelikereadnode_modulesexpresslibrouterindex.js:315:13) at D:nodelikereadnode_modulesexpresslibrouterindex.js:284:7 at Function.process_params (D:nodelikereadnode_modulesexpresslibrouterindex.js:335:12) at Immediate.next (D:nodelikereadnode_modulesexpresslibrouterindex.js:275:10) at Immediate.<anonymous> (D:nodelikereadnode_modulesexpresslibrouterindex.js:635:15) at runCallback (timers.js:668:20) at tryOnImmediate (timers.js:639:5)
问题解答
回答1:connection.disconnect((err,res)=>{ if(err)console.log(err);});*/ res.render(’user.pug’);
你这是什么鬼? res.render(’user.pug’);放到函数外面?
回答2:render和disconnect应该放到save完成的回调函数内部执行
相关文章:
1. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?2. css - 关于input标签disabled问题3. javascript - 关于一段 for 循环代码执行顺序的问题4. javascript - 静态页面引公共头尾文件,js怎么写吖?5. debian - docker依赖的aufs-tools源码哪里可以找到啊?6. docker api 开发的端口怎么获取?7. javascript - vue 数据更新了。但是dom没有更新,,,,,如图8. java - 为什么要将Runnable接口的子类对象传递给Thread的构造函数?9. 如何调整IDEA中直接运行java程序的日志输出级别?10. java基础,求解答。