【发布时间】:2018-03-19 02:20:10
【问题描述】:
在我的主文件 server.js 中,我有以下功能:
server.js
const mongoose = require('mongoose');
const SmallRounds = require('./models/smallrounds.js');
function initRound(){
logger.info('Initializing round...');
SmallRounds.getLatestRound((err, data) => {
[...]
});
}
函数 getLatestRound() 被导出到我的猫鼬模型 smallrounds.js 中
smallrounds.js
const mongoose = require('mongoose');
const config = require('../config.js');
const SmallRoundsSchema = mongoose.Schema({
[...]
});
const SmallRounds = module.exports = mongoose.model('SmallRounds', SmallRoundsSchema);
module.exports.getLatestRound = function(callback){
SmallRounds.findOne().sort({ created_at: -1 }).exec((err, data) => {
if(err) {
callback(new Error('Error querying SmallRounds'));
return;
}
callback(null, data)
});
}
但是当我调用 initRound() 时出现以下错误:
TypeError: SmallRounds.getLatestRound 不是函数
在 initRound (E:\Projects\CSGOOrb\server.js:393:14)
在 Server.server.listen (E:\Projects\CSGOOrb\server.js:372:2)
在 Object.onceWrapper (events.js:314:30)
在 emitNone (events.js:110:20)
在 Server.emit (events.js:207:7)
在 emitListeningNT (net.js:1346:10)
在 _combinedTickCallback (internal/process/next_tick.js:135:11)
在 process._tickCallback (internal/process/next_tick.js:180:9)
在 Function.Module.runMain (module.js:607:11)
启动时 (bootstrap_node.js:158:16)
在 bootstrap_node.js:575:3
为什么会这样?我不认为我有循环依赖,也没有拼错任何东西。谢谢:)
【问题讨论】:
-
可能
mongoose.model中返回的对象被冻结了还是什么?确保您需要的文件与本文中的完全一致。 -
是的,我复制粘贴了代码
标签: javascript node.js mongoose