[英]Can't display data with MongoDB, Mongoose, Node.js and Express in CoffeeScript
I've got a Node.js, Express setup with Mongo and Mongoose written in CoffeeScript. 我已经用CoffeeScript编写了一个Node.js,使用Mongo和Mongoose的Express设置。
I can save data into my collection with this code: 我可以使用以下代码将数据保存到我的集合中:
# new
app.get "/admin/new", (req, res) ->
res.render "admin/new.jade", locals: c: new Content()
# create
app.post "/admin.:format?", (req, res) ->
content = new Content(req.body["content"])
content.save ->
switch req.params.format
when "json"
res.send content.__doc
else
res.redirect "/"
But I can't display any data with this code. 但我无法使用此代码显示任何数据。 The web browser just says "Waiting for localhost..." and the console says nothing.
网络浏览器只是说“等待本地主机......”而控制台什么也没说。
# index
app.get "/admin.:format?", (req, res) ->
Content.find().all (contents) ->
switch req.params.format
when "json"
res.send contents.map((c) ->
c.__doc
)
else
res.render "admin/index.jade", locals: contents: contents
I am taking a look at Moongoose documentation. 我正在看Moongoose文档。
The querying documentation says if you do not specify a callback, you can use the Query object to further refine your search, and the query object does not seem to specify a "all" function. 查询文档说,如果不指定回调,则可以使用Query对象进一步优化搜索,并且查询对象似乎没有指定“ all”函数。 Is your code silently failing?
您的代码是否默默失败?
I believe what you are really looking for is: 我相信你真正想要的是:
Content.find({}).exec (err, contents) ->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.