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.
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 localho开发者_如何学JAVAst..." 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.
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. Is your code silently failing?
I believe what you are really looking for is:
Content.find({}).exec (err, contents) ->
精彩评论