【问题标题】:AngularJS, ExpressJS and MongooseAngularJS、ExpressJS 和猫鼬
【发布时间】:2013-06-21 04:25:05
【问题描述】:

我已经成功创建了一个将 JSON 数据发送到我的 AngularJS 应用程序的 api。后端从 Mongodb 读取数据(使用 Mongoose 包)。 index 方法就像一个魅力(我使用 jsonp,因为我的测试环境是一台服务器,后端和前端运行在不同的端口上)

exports.index = function (req, res){
    return ContactModel.find(function (err, contacts) {
        if (!err) {
            return res.jsonp(contacts);
        } else {
                return console.log(err);
        }
    });
}

AngularJS 部分如下所示:

$http({method: 'jsonp', url: 'http://host:1222/contacts?callback=JSON_CALLBACK'}).success(function(data, status, headers, config) {
    $scope.contacts = data;
});

稍后我可以愉快地访问这些数据(例如 {{ contact.name }}

问题是当我尝试只查看一个结果时,使用 findById:

exports.findById = function (req, res) {
    return ContactModel.findById(req.params.id, function (err, contact) {
      if (!err) {
            return res.jsonp(contact);
      } else {
        return console.log(err);
      }
    });
}

我的 AngularJS ViewController 看起来像这样:

function ViewController($scope, $http) {
    $http({method: 'jsonp', url: 'http://host:1222/contacts/:id?callback=JSON_CALLBACK'}).success(function(data, status, headers, config) {
      console.log("Data: " +data);
      $scope.contact = data;
    });
}

它的调用者是:

<a href="#/contacts/{{ contact._id }}">{{ contact.name }}</a>

但是,我一直收到的错误是:

{ message: 'Cast to ObjectId failed for value ":id" at path "_id"',
  name: 'CastError',
  type: 'ObjectId',
  value: ':id',
  path: '_id' }

这是数据库中的一个示例:

JSON_CALLBACK && JSON_CALLBACK([
  {
    "name": "Test User",
    "_id": "51c5fde3ce36618e0c000003",
    "__v": 0
  }
]);

我已经阅读了很多关于“Cast to ObjectId failed for value ":id" at path "_id"" 问题的文章,但我不明白...我需要为查找?在这种情况下,我必须引入一个 auto_increment 唯一的 ID-ing 模式,这不推荐用于 MongoDB,因此,您能否告知我必须做些什么才能以正确的方式查询数据?此外,如果您在 AngularJS 方面发现我当前的实现有任何问题,请告诉我(我是该主题的新手。)。

更新

这就是我使用 AngularJS 进行路由的方式:

  angular.module('contactmanager', ['filters']).
        config(function($routeProvider) {
          $routeProvider.
            when('/', {controller:ListController, templateUrl:'list.html'}).
            when('/contacts/:id', {controller:ViewController, templateUrl:'contact.html'}).
            otherwise({redirectTo:'/'});
        });

更新 2

服务器端路由——快递:

var express = require("express");
var contacts = require("./contact");
var app = express();

app.get("/", contacts.index);
app.get('/contacts', contacts.index);
app.get('/contacts/:id', contacts.findById);
app.listen(1222);

我认为这里缺少一些东西......?

【问题讨论】:

    标签: angularjs mongodb express mongoose


    【解决方案1】:

    我假设消息是 mongo 的服务器端?

    消息意味着_id的:id而不是51c5fde3ce36618e0c000003,当然:id字符串不是有效的ObjectId

    编辑:所以上面是正确的,在你的控制器中,你的 ajax 调用点击了这个 url:http://host:1222/contacts/:id?callback=JSON_CALLBACK你的 :id 没有参数化,它是硬编码的,并作为一个值传递。

    你需要使用$routeParams来解释它的值:

    function ViewController($scope, $http, $routeParams) {
        var ajax_url = 'http://host:1222/contacts/' + $routeParams.id + '?callback=JSON_CALLBACK';
        $http({method: 'jsonp', url: ajax_url}).success(function(data, status, headers, config) {
          console.log("Data: " +data);
          $scope.contact = data;
        });
    }
    

    【讨论】:

    • 啊其实我是在问快递应用的服务器端路由
    • 再次更新,尽管我敢打赌,由于我没有太多通过 express 进行的路由,所以缺少一些东西。
    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 2018-09-30
    • 2015-07-17
    • 2016-12-26
    • 1970-01-01
    • 2015-03-17
    • 2022-07-09
    • 2012-01-17
    相关资源
    最近更新 更多