【问题标题】:Can't display data with MongoDB, Mongoose, Node.js and Express in CoffeeScript无法在 CoffeeScript 中使用 MongoDB、Mongoose、Node.js 和 Express 显示数据
【发布时间】:2011-12-07 03:08:16
【问题描述】:

我有一个 Node.js,用 CoffeeScript 编写的带有 Mongo 和 Mongoose 的 Express 设置。

我可以使用以下代码将数据保存到我的集合中:

# 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 "/"

但我无法使用此代码显示任何数据。网络浏览器只是说“等待本地主机...”而控制台什么也没说。

# 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

【问题讨论】:

    标签: mongodb node.js coffeescript express mongoose


    【解决方案1】:

    我正在查看 Moongoose 文档。

    querying documentation 表示如果不指定回调,可以使用Query object 进一步细化搜索,查询对象似乎没有指定“全部”函数。您的代码是否静默失败?

    我相信你真正想要的是:

    Content.find({}).exec (err, contents) ->
    

    【讨论】:

    猜你喜欢
    • 2011-12-07
    • 2020-01-13
    • 2015-06-29
    • 2012-02-07
    • 2019-05-17
    • 2012-12-04
    • 2012-12-09
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多