Node.js 错误:对象函数没有方法

发布于 2024-12-17 06:32:12 字数 4904 浏览 0 评论 0原文

我刚刚开始接触 Node.js,并按照 Howtonode、express 和 mongod 上关于 Node.js 的精彩教程进行操作。但是,我似乎收到了一个错误,而评论中没有其他人评论过。最新评论是大约一个月前,所以代码可能已经过时了?

问题是,当我访问 http://localhost:3000/ 时,页面显示 Internal Server Error 并且在终端中,我收到以下错误消息。有谁知道发生了什么事吗?

这是错误消息:

TypeError: Object function (){} has no method 'findAll'
    at Router.<anonymous> (/Users/x/nodejs/howtonode/blog/app.js:30:18)
    at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
    at middleware (/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)
    at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
    at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
TypeError: Object function (){} has no method 'findAll'
    at Router.<anonymous> (/Users/x/nodejs/howtonode/blog/app.js:30:18)
    at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
    at middleware (/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)
    at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
    at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
    at next (/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);

article-provider-memory.js

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;

I just started getting into node.js and followed this great tutorial on node.js at Howtonode, express and mongod. However, I seem to be getting an error that no one else commented on in the comments. The latest comment was about a month ago, so maybe the code is outdated?

The problem is that when I visit http://localhost:3000/, the page shows Internal Server Error and in the terminal, I get the error message below. Does anyone know what happened?

Here's the error message:

TypeError: Object function (){} has no method 'findAll'
    at Router.<anonymous> (/Users/x/nodejs/howtonode/blog/app.js:30:18)
    at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
    at middleware (/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)
    at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
    at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
TypeError: Object function (){} has no method 'findAll'
    at Router.<anonymous> (/Users/x/nodejs/howtonode/blog/app.js:30:18)
    at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
    at middleware (/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)
    at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
    at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
    at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
    at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
    at next (/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);

article-provider-memory.js

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-12-24 06:32:12

这是 app.jsArticleProvider 的大写形式:

// 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);
    });
});

It's your capitalization of ArticleProvider here in app.js:

// Routes
app.get('/', function(req, res) {
    ArticleProvider.findAll(function(error, docs) {
        res.send(docs);
    });
});

It should be:

// Routes
app.get('/', function(req, res) {
    articleProvider.findAll(function(error, docs) {
        res.send(docs);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文