Mongoose没有将数据保存到mongodb

时间:2013-11-23 15:16:14

标签: node.js mongodb coffeescript mongoose

我想记录每次计算,但是cal.save()没有在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

另外,为了检查它是否已保存,我转到终端并键入mongo,然后输入db.calculation.find()但没有出现任何内容:

1 个答案:

答案 0 :(得分:2)

Mongoose将您的小写型号名称复数以确定要使用的集合。所以你需要查看calculations集合。

db.calculations.find()