Javascript Node.js错误:对象函数没有方法
我刚刚开始进入node.js,并在、express和mongod上学习了这篇关于node.js的精彩教程。然而,我似乎得到了一个没有人在评论中评论的错误。最近的评论大约是一个月前,所以代码可能已经过时了 问题是当我访问Javascript Node.js错误:对象函数没有方法,javascript,node.js,express,Javascript,Node.js,Express,我刚刚开始进入node.js,并在、express和mongod上学习了这篇关于node.js的精彩教程。然而,我似乎得到了一个没有人在评论中评论的错误。最近的评论大约是一个月前,所以代码可能已经过时了 问题是当我访问http://localhost:3000/,页面显示内部服务器错误,在终端中,我得到下面的错误消息。有人知道发生了什么吗 以下是错误消息: TypeError:对象函数(){}没有方法“findAll” 在路由器。(/Users/x/nodejs/howtonode/blog/a
http://localhost:3000/
,页面显示内部服务器错误
,在终端中,我得到下面的错误消息。有人知道发生了什么吗
以下是错误消息:
TypeError:对象函数(){}没有方法“findAll”
在路由器。(/Users/x/nodejs/howtonode/blog/app.js:30:18)
完成时(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
在中间件上(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:244:9)
at param(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:227:11)
通过时(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
路由器调度(/Users/x/nodejs/howtonode/blog/node\u modules/express/lib/Router/index.js:255:4)
在Object.handle(/Users/x/nodejs/howtonode/blog/node\u modules/express/lib/router/index.js:45:10)
接下来(/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
在Object.handle(/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
接下来(/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
TypeError:对象函数(){}没有方法“findAll”
在路由器。(/Users/x/nodejs/howtonode/blog/app.js:30:18)
完成时(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
在中间件上(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:244:9)
at param(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:227:11)
通过时(/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
路由器调度(/Users/x/nodejs/howtonode/blog/node\u modules/express/lib/Router/index.js:255:4)
在Object.handle(/Users/x/nodejs/howtonode/blog/node\u modules/express/lib/router/index.js:45:10)
接下来(/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
在Object.handle(/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
接下来(/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
app.js
var express = require('express');
var ArticleProvider = require('./articleprovider-memory').ArticleProvider;
var app = module.exports = express.createServer()
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStakc: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
var articleProvider= new ArticleProvider();
// Routes
app.get('/', function(req, res) {
ArticleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
app.listen(3000);
var articleCounter = 1;
ArticleProvider = function(){};
ArticleProvider.prototype.dummyData = [];
ArticleProvider.prototype.findAll = function(callback) {
callback( null, this.dummyData )
};
ArticleProvider.prototype.findById = function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
result = this.dummyData[i];
break;
}
}
callback(null, result);
};
ArticleProvider.prototype.save = function(articles, callback) {
var article = null;
if( typeof(articles.length)=="undefined")
articles = [articles];
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article._id = articleCounter++;
article.created_at = new Date();
if( article.comments === undefined )
article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
this.dummyData[this.dummyData.length]= article;
}
callback(null, articles);
};
/* Lets bootstrap with dummy data */
new ArticleProvider().save([
{title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
{title: 'Post two', body: 'Body two'},
{title: 'Post three', body: 'Body three'}
], function(error, articles){});
exports.ArticleProvider = ArticleProvider;
文章提供者memory.js
var express = require('express');
var ArticleProvider = require('./articleprovider-memory').ArticleProvider;
var app = module.exports = express.createServer()
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStakc: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
var articleProvider= new ArticleProvider();
// Routes
app.get('/', function(req, res) {
ArticleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
app.listen(3000);
var articleCounter = 1;
ArticleProvider = function(){};
ArticleProvider.prototype.dummyData = [];
ArticleProvider.prototype.findAll = function(callback) {
callback( null, this.dummyData )
};
ArticleProvider.prototype.findById = function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
result = this.dummyData[i];
break;
}
}
callback(null, result);
};
ArticleProvider.prototype.save = function(articles, callback) {
var article = null;
if( typeof(articles.length)=="undefined")
articles = [articles];
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article._id = articleCounter++;
article.created_at = new Date();
if( article.comments === undefined )
article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
this.dummyData[this.dummyData.length]= article;
}
callback(null, articles);
};
/* Lets bootstrap with dummy data */
new ArticleProvider().save([
{title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
{title: 'Post two', body: 'Body two'},
{title: 'Post three', body: 'Body three'}
], function(error, articles){});
exports.ArticleProvider = ArticleProvider;
var-articleccounter=1;
ArticleProvider=函数(){};
ArticleProvider.prototype.dummyData=[];
ArticleProvider.prototype.findAll=函数(回调){
回调(null,this.dummyData)
};
ArticleProvider.prototype.findById=函数(id,回调){
var结果=null;
对于(var i=0;i这是您在app.js
中对ArticleProvider
的大写:
// Routes
app.get('/', function(req, res) {
ArticleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
应该是:
// Routes
app.get('/', function(req, res) {
articleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
//路由
app.get('/',函数(req,res){
articleProvider.findAll(函数(错误,文档){
res.send(文档);
});
})
这是你在app.js
中的ArticleProvider
的大写字母:
// Routes
app.get('/', function(req, res) {
ArticleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
应该是:
// Routes
app.get('/', function(req, res) {
articleProvider.findAll(function(error, docs) {
res.send(docs);
});
});
//路由
app.get('/',函数(req,res){
articleProvider.findAll(函数(错误,文档){
res.send(文档);
});
});
@Nyxynyx,把一半头发捐给minitech。@Nyxynyx,把一半头发捐给minitech。