2016-07-11 5 views
0

현재 간단한 프로젝트를 진행 중입니다. 몽고 데이터베이스에서 항목 하나를 가져 와서 웹 페이지에 표시하려고합니다. 개체에 대한 정보를 보려면 http://localhost:3000/api/events/5782b1dbb530152d0940a227 사이트에 갈 때마다 null이 표시됩니다. 내가 말했듯이, 나는이 물건에 대한 정보를 보려고한다.MongoDB, Node 및 Express-looking with Mongo ID 문제

컨트롤러 :

var mongoose = require('mongoose'); 
var Eve = mongoose.model('Info'); 

var sendJSONresponse = function(res, status, content) { 
res.status(status); 
res.json(content); 
}; 

module.exports.eventsReadOne = function(req, res) { 
Eve 
    .findById(req.params.eventid) 
    .exec(function(err, info){ 
    sendJSONresponse(res, 200, info) 
    }); 
    //sendJSONresponse(res, 200, {"status" : "success"}); 
}; 

모델 :

var mongoose = require('mongoose') 

var eventSchema = new mongoose.Schema({ 
    activity: String, 
    address: String, 
}); 

mongoose.model('Info', eventSchema); 

경로 : 여기에 내 코드와 같은 모습입니다 이제

var express = require('express'); 
var router = express.Router(); 
var ctrlEvents = require('../controllers/events'); 

//events 
router.get('/events/:eventid', ctrlEvents.eventsReadOne); 

module.exports = router; 

, 발견 할 수있다 한 가지입니다 그 I 내 몽고 컬렉션 이벤트에 전화 해. 그러나 이벤트가 JS의 핵심어라는 것을 잊어 버렸으므로 Info 모델을 "변경"하여 내 모델의 마지막 줄에 표시하려고했습니다. 내가 말했듯이, 내가 마지막으로 숫자가 obj _id 인 곳인 http://localhost:3000/api/events/5782b1dbb530152d0940a227에 가면 나는 그것에 관한 모든 데이터를보아야한다. 대신, 내가 보는 모든 것은 null입니다. 어떤 도움이라도 좋을 것입니다, 감사합니다!

var mongoose = require('mongoose'); 
var dbURI = 'mongodb://localhost/mission'; 
mongoose.connect(dbURI) 

require('./events'); 
+0

'console.log (req.params)'를 어떻게 돌보고 있습니까? – kawadhiya21

+0

네, 아무 일도 일어나지 않았다. 나는 그것을 sendJSON 응답 바로 위에 놓았 는가? –

+0

'event'는 JS에서 키워드가 아닙니다. 오류 처리를 추가하십시오. 또한, 당신은'mongoose.connect()'를 코드의 어딘가에서 호출한다고 가정합니다. – robertklep

답변

0

몽구스 그것을 사용해야하는 MongoDB의 컬렉션을 결정하는 모델 이름을 사용합니다 :

내 다른 모델 파일, db.js는 연결이 있습니다. 당신이 그것을 원하는 경우

// This is where the model is created from the schema, and at this point 
// the collection name is decided. 
mongoose.model('Info', eventSchema); 

: 기본 방식은 소문자, 모델명을하고 복수형 및 infos 것입니다 귀하의 경우 (모델 이름으로 Info 사용) 컬렉션 이름과 같은 결과를 사용하는 것입니다 당신이 당신의 스키마에 대한 collection 옵션을 설정하여 사용하는 어떤 컬렉션을 명시 적으로 몽구스 말할 필요가 다른 모음을 사용 :

var eventSchema = new mongoose.Schema({ 
    activity : String, 
    address : String, 
}, { collection : 'events' }); 

당신이 의견 진술하고있는 오류를 수정하려면 (는 스키마되지 않았습니다 모델 "mission"에 등록)), 당신 당신이 mongoose.model()의 모든 항목을 변경하는 것이 있는지 확인해야합니다 : "임무는"당신의 데이터베이스의 이름임을 것으로 보인다 있지만 (

// To create the model: 
mongoose.model('Mission', eventSchema); 

// Later on, to access the created model from another part of your code: 
var Eve = mongoose.model('Mission'); 

을; 당신의 콜렉션이 이벤트이라고 생각하기 때문에 모델명이 Event 인 것 같아요.

+0

굉장해! 너 남자 야! 그것은 작동합니다! 이 사람을 의미있는 스택으로 시작하는 데 도움을 주셔서 감사합니다! –