2013-11-23 67 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

相关问题