2013-11-23 103 views
0

我想記錄所做的每個計算,但cal.save()沒有在mongodb中註冊數據對象。目前它只是阻止我的貓鼬模塊):任何善良的靈魂都願意看看我簡單的代碼。Mongoose不保存數據到mongodb

# Import library files 
restify   = require 'restify' 
mongoose  = require 'mongoose' 

# Create schema for recording calculations 
calculationSchema = new mongoose.Schema 
    action: String 
    firstNumber: Number 
    secondNumber: Number 
    timeStamp: 
     type: Date 
     default: Date.now 

Calculation = mongoose.model 'calculation', calculationSchema 

# Start and verify mongodb connection 
mongoose.connect 'mongodb://localhost/test' 
db = mongoose.connection 
db.on 'error', console.error.bind console, 'connection error:' 
db.once 'open', -> 
    console.log "Successfully connected to MongoDB" 


# Create API server to listen to requests 
server = restify.createServer() 

# Create GET API that capture all params in the urls 
server.get '/calculate/:action/:firstNum/:secondNum', (req, res, next) -> 
    action = req.params.action 
    firstNum = parseFloat req.params.firstNum 
    secondNum = parseFloat req.params.secondNum 

    # Create Calculation object 
    cal = new Calculation 
     action: action 
     firstNumber: firstNum 
     secondNumber: secondNum 

    cal.save (err, cal) -> 
     # Handle Errors 
     if err then console.log 'Erorr in saving calculation' 

    res.send 200 

此外,以檢查它是否保存,我到終端和類型蒙戈其次db.calculation.find(),但一無所獲):

+0

有什麼不合適的呢? – JohnnyHK

+0

哦,不,我離開了那部分。它mongodb沒有註冊cal.save()方法。): – Junhao

回答

2

貓鼬複數化您的小寫的型號名稱確定要使用的集合。所以你需要看看calculations集合。

db.calculations.find() 
+0

該死的...感覺已經很笨... – Junhao

+0

@Junhao不要,它絆倒了很多人,這是作者公認的錯誤但我們堅持下去。 – JohnnyHK

+0

無論如何,非常感謝您的回答!:D – Junhao

相關問題