2013-06-24 69 views
0

我已經成功創建了一個將JSON數據發送到我的AngularJS應用程序的api。後端從Mongodb中讀取數據(使用Mongoose包)。該指數方法的工作原理就像一個魅力(我使用JSONP作爲我的測試環境是一臺服務器,以在不同的端口上運行的後端&前端)AngularJS,ExpressJS和Mongoose

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視圖控制器看起來就像這樣:

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 
    } 
]); 

我已閱讀有關「演員到的ObjectId失敗值‘好幾篇文章:ID’的路徑‘_id’」的問題,但我不明白這一點...我是否需要爲查找創建自己的ID?在這種情況下,我必須引入一個auto_increment唯一標識模式,因此不建議在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); 

我認爲這裏有一些東西是缺少的......?

回答

2

我假設消息是由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; 
    }); 
} 
+0

我會更新上面的路由職。 – Tamas

+0

啊實際上我是要求服務器端路由的快速應用 –

+0

再次更新,雖然我敢打賭有一些缺失,因爲我沒有太多的路由通過快遞進行。 – Tamas

相關問題