Javascript mongoose函数仅在第二次调用时工作
所以最奇怪的事情发生了。我有一个Javascript mongoose函数仅在第二次调用时工作,javascript,node.js,mongodb,coffeescript,mongoose,Javascript,Node.js,Mongodb,Coffeescript,Mongoose,所以最奇怪的事情发生了。我有一个coffeescript类,用于构建猫鼬模式 module.exports = class Profit constructor: (Schema, mongoose) -> @DailyProfitSchema = new Schema( profit: type: Number default: 0 percen
coffeescript
类,用于构建猫鼬模式
module.exports = class Profit
constructor: (Schema, mongoose) ->
@DailyProfitSchema = new Schema(
profit:
type: Number
default: 0
percent_profit:
type: Number
default: 0
day:
type: Number
default: 0
)
@Day = mongoose.model('Day', @DailyProfitSchema)
@local_day = null
这是我的更新功能
update: (funds) ->
if @local_day?.day is not date.getDate()
# new day
@local_day = new @Day()
@local_day.save(
(err, res) ->
console.log "saving local day"
if err?
console.log err
)
奇怪的是当我更新
Profit = require '../new_profit'
mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test')
Schema = mongoose.Schema
profit = new Profit(Schema, mongoose)
funds =
amount: 100
profit.update(funds)
不输出任何内容,这与调用@local_day.save()
时应输出的预期保存本地日不同
好吧,这已经够奇怪了,但更奇怪的是,当我将更新函数改为
update: (funds) ->
if @local_day?.day is not date.getDate()
# new day
@local_day = new @Day()
console.log @local_day.save() # this is the line I added
@local_day.save(
(err, res) ->
console.log "saving local day"
if err?
console.log err
)
我得到了更意想不到的结果
undefined
saving local day
这可能是什么原因造成的
我也不确定它是否相关,但我正在运行mongod
作为后台进程,以使本地db
工作
更新:不管是什么问题,当我停止使用本地dbmongodb://localhost/test
在有效的代码中,您还添加了或not@local\u day?
,确定不是这样吗?另外,属性day
未在您的架构中声明。@robertklep我确信不是这些,但谢谢