Tar*_*len 4 mongoose node.js express
我已经定义了这条路线,但是对它的任何请求都会被"挂起"并且永远运行.
当我记录代码时,我看到1后面跟着4,这意味着find方法中的代码永远不会被执行
# Calendar routes
router.get '/calendars', (req, res) ->
console.log '1'
Calendar.find (err, calendars) ->
console.log "2" + err
console.log "3" + calendars
res.send(err) if err
res.json(calendars)
return
console.log '4'
return
Run Code Online (Sandbox Code Playgroud)
模型
mongoose = require("mongoose")
module.exports = mongoose.model("Calendar",
name: String
)
Run Code Online (Sandbox Code Playgroud)
有关为什么会这样的想法?
在你打电话之前mongoose.connect,你的mongoose查询将排队等候.
在你的启动代码中添加这样的代码来连接:
mongoose.connect('mongodb://localhost/test', function(err) {
if (err) {
console.err(err);
} else {
console.log('Connected');
}
});
Run Code Online (Sandbox Code Playgroud)
在连接字符串中,替换test为数据库的名称.
| 归档时间: |
|
| 查看次数: |
4138 次 |
| 最近记录: |