2013-10-21 3 views
0

express.js, backbone.js 응용 프로그램에서 mongodb 찾기의 '내보내기'메소드를 통해 요청 매개 변수를 보내려고합니다. 나는 어려운 매개 변수를 얻기 위해 mongodb와 '#'을 통과하는 데 어려움을 겪고있다.MongoDB가 req.params를 내보내기 함수로 전달하는

파손은 내 보낸 mongodb 함수에 매개 변수를 전달하는 것입니다.

먼저 요청이 성공적 '곧'기능으로 전달된다 :

"upcoming/uni/:uni" : "upcoming", 

이는 흐름의 "이번 '기능에 문제없이 여기

데이터의 흐름이다.

upcoming: function(uni) { 
    console.log("uni: "+uni); 
    pag.reset(); 
    console.log("Hit upcoming list target"); 
    setCollectionType('upcoming'); 
     var upcomingCourses = buildCollection(); 

    // ------------------------------------------------------------------------ 
    // here is the problem how do I pass the parameter value through the fetch? 
    // Although it may also have to do with '#' please read on. 
    // ------------------------------------------------------------------------ 
     upcomingCourses.fetch({success: function(){ 
      $("#content").html(new ListView({model: upcomingCourses, page: 1}).el); 
     }}); 
    this.headerView.selectMenuItem('home-menu'); 
}, 

몽고 방법에 대한 라우팅은 다음과 같습니다

app.get('/upcoming/uni/:uni', mongomod.findUpcoming); 

그래서 다음과 같은 방법은 MongoDB를 JS 파일에서 내 보낸하고 신뢰할 수있는 실행됩니다. 그러나 req.params는 전달되지 않습니다. 내가 그 '런타임 동작 설명 한 코드에 산재 : 추가 및 중요 사항에

exports.findUpcoming = function(req, res) { 
    console.log("university", req.params.uni); // This consistently is unpopulated 
    var uni = req.params.uni; 
    console.log("Size: "+req.params.length); // This will always be 0 
    for (var i=0; i < req.params.length; i++) { 
     console.log("Parameters: "+req.params[i]); 
    } 

    db.collection('upcoming', function(err, collection) { 

    if (typeof uni === 'undefined') { 
     console.log("The value is undefined"); 
     uni = "Princeton University"; // here we add a string to test it it will work. 
    } 

    collection.find({university:uni}).toArray(function(err, items) { 
     if (err) { 
      console.log("Error: "+err); 
     } else { 
      console.log("No Error"); 
      console.log("Count: "+items.length); 
      console.log(items[0]['university']); 
      res.send(items); 
     } 
    }); 
    }); 
}; 

을 :

URL입니다, 작업에서, 런타임 환경은 다음과 같습니다

http://localhost:3000/#upcoming/uni/Exploratorium 

이 하나는 실패하지만 다음 URL은이 함수를 통해 매개 변수를 전달하는 데 사용되지만 렌더링 된 버전이 아닌 JSON을 반환합니다.

http://localhost:3000/upcoming/uni/Exploratorium 

#과 템플릿에 대한 이해가 부족할 수 있습니다. 제발, 당신이 오류 계몽을 볼 수 있다면 크게 감사하겠습니다.

+0

http://stackoverflow.com/questions/9987045/how-to-get-hash-in-a-server-side-language –

답변

0

클라이언트 측에서 서버 측으로 매개 변수를 전달하는 문제에 대한 해결책을 찾았습니다. 매개 변수가 서버 측에 전달됩니다 컬렉션의 URL을 변경하여 :

upcomingCourses.url = "/upcoming/uni/"+uni; // <-- here's the ticket where uni is param 
    upcomingCourses.fetch({success: function(){ 
     $("#content").html(new ListView({model: upcomingCourses, page: 1}).el); 
}}); 

이 더 우아 할 수 있지만 서버에 매개 변수를 전달하는 방법입니다.

감사

관련 문제