Javascript Node.js | TypeError:[…]不是函数
在我的主文件server.js中,我有以下功能: server.jsJavascript Node.js | TypeError:[…]不是函数,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,在我的主文件server.js中,我有以下功能: server.js const mongoose = require('mongoose'); const SmallRounds = require('./models/smallrounds.js'); function initRound(){ logger.info('Initializing round...'); SmallRounds.getLatestRound((err, data) => {
const mongoose = require('mongoose');
const SmallRounds = require('./models/smallrounds.js');
function initRound(){
logger.info('Initializing round...');
SmallRounds.getLatestRound((err, data) => {
[...]
});
}
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)
});
}
函数getLatestRound()在我的mongoose模型smallrounds.js中导出
smallrounds.js
const mongoose = require('mongoose');
const SmallRounds = require('./models/smallrounds.js');
function initRound(){
logger.info('Initializing round...');
SmallRounds.getLatestRound((err, data) => {
[...]
});
}
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)
在EmitListengNT(net.js:1346:10)
at_combinedTickCallback(内部/流程/下一步_tick.js:135:11)
在进程中。_tickCallback(内部/进程/下一个_tick.js:180:9)
位于Function.Module.runMain(Module.js:607:11)
启动时(bootstrap_node.js:158:16)
在bootstrap_node.js:575:3
为什么会这样?我不认为我有循环依赖关系,也没有拼写错误。谢谢:)这不是向Mongoose模型/模式添加方法的方式 试试这个:
const mongoose = require('mongoose');
const config = require('../config.js');
const SmallRoundsSchema = mongoose.Schema({
[...]
});
SmallRoundsSchema.statics.getLatestRound = function(callback){
this.findOne().sort({ created_at: -1 }).exec((err, data) => {
if(err) {
callback(new Error('Error querying SmallRounds'));
return;
}
callback(null, data)
});
}
const SmallRounds = module.exports = mongoose.model('SmallRounds', SmallRoundsSchema);
您可以在这里阅读文档:,在“静态”部分。还有其他更好的方法可以达到同样的效果,但这会让你开始学习。我在使用大写模块,得到错误
TypeError:upperCase不是一个函数
l
每一篇教程都是这样写的
我把它改成了
res.end(upperCase.upperCase("Hello World"));
工作正常可能是
mongoose.model
中返回的对象冻结了还是什么?确保你需要的文件和这篇文章中的完全一样。是的,我复制粘贴了代码。谢谢。我真的不知道我的代码发生了什么,我已经为这些函数使用module.exports好几个月了,除了今天它突然停止工作之外,从来没有遇到过问题。