2013-07-22 2 views
1
내가 로컬 Node.js를, 익스프레스, 몽구스와 MongoDB를 실행하고 내 app.js이 코드가있어

:Node.js app.post가 MongoDB를 채우지 않습니까?

var express = require('express'); 
var app = express(); 
app.use(express.bodyParser()); 

// Decaler Mongoose 
var mongoose = require('mongoose'); 
mongoose.connect('localhost','tinyreports'); 

// Require the Report model 
var models = require('./models/reports.js'); 

app.post('/report/add', function(req, res) { 
    var title = req.body.title; 
    var recipients = req.body.recipients; 

    var report = new models.Report({title:title}); 
    report.save(function(err, report) { 
     if (err) return handleError(err); 
     res.json({title:report.title}); 
    }); 

}); 

app.listen(3000); 

console.log('server_start'); 

나는 또한 다음과 같은 모델 (Reports.js에)가 :

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var reportsSchema = new Schema({ 
    title: String 
}); 

exports.Report = mongoose.model('Report', reportsSchema); 

cURL 게시 curl -X POST -H "Content-Type:application/json" -d '{"title":"test"}' http://localhost:3000/report/add을 실행하여 데이터베이스를 채울 때 아무 일도 발생하지 않습니다.

내가 뭘 잘못하고 있니? 내 코드 또는 전체 설정에 문제가 있습니까?

감사합니다.

+0

명백한 문제는 없습니다. DB 내용을 어떻게 확인하고 있습니까? '/ report/add' 내부에서'console.log' 호출을 시도하여 예상대로 작동하는지 확인하십시오. –

+0

방금 ​​시도했는데, 코드가 잘 작동하고, 모든 libs를 최신 버전으로 업그레이드하려고 시도했을까요? – zsong

+0

@PeterLyons 제목과 함께 res.json을 수행하면 작동하기 때문에 멋진 배열을 반환합니다. mongo 쉘에서 db.tinyreports.save ({title : "test"})를 수행하면 데이터베이스가 잘 채워집니다. 하지만 내 코드를 실행할 때 아무 것도. 이상한 ... – Ismailp

답변

-1

그래서 나는 알아 냈고 내 코드에는 아무런 문제가 없었지만 MongoDB를 처음 접했을 때 데이터베이스의 잘못된 위치를 조사 했으므로 항목을 찾을 수 없었습니다 ... DOH!

관련 문제