【问题标题】:get mongodb model schema in angular controller在角度控制器中获取 mongodb 模型模式
【发布时间】:2015-01-26 09:30:29
【问题描述】:

如何在角度控制器中获取 mongodb 模型架构为 json?

我试过了:

`

exports.getSchema = function(res) {
  Info.find().lean().exec(function (err, infos) {
    return res.send(JSON.stringify(infos));
  });
};

`

和控制器:

`

$scope.getSchema = function() {
      $http.get('/infos/schema')
      .success(function(data) {
            $scope.schema = angular.toJson(data);
        });
    };

`

但我得到500 internal server error

CastError: Cast to ObjectId failed for value "schema" at path "_id"

正确的方法是什么?

【问题讨论】:

  • 你试过console.log(err)吗?
  • 我会编辑帖子。谢谢
  • 错误说明了一切,您有“_id”键的值“模式”,它应该是一个 ObjectId。您可以使用 Robomongo 连接到您的数据库以轻松查看数据。 robomongo.org

标签: json angularjs node.js mongodb schema


【解决方案1】:

我打赌你正在尝试在你的 req 对象上使用 res.send。

修改你的节点函数:

exports.getSchema = function(req, res) {

【讨论】:

    【解决方案2】:

    我找到了解决方案。 为此目的有一个 github 存储库,它从 Mongoose 模式 mongoose-schema 中创建一个 json

    看看:Google Group of mongoose: Return a mongoose schema as JSON

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多