我想記錄所做的每個計算,但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(),但一無所獲):
有什麼不合適的呢? – JohnnyHK
哦,不,我離開了那部分。它mongodb沒有註冊cal.save()方法。): – Junhao