TypeError:对象不是Node.js函数

用户名

我正在Lynda.com上进行Node.js基本培训...

跟随视频,但我在终端中收到此错误。

“ TypeError:对象不是函数”

node_modules / flight / index.js

var count = 0,
    destinations = [];

var Flight = function () {
        this.data = {
        number : null,
        origin: null,
        destination: null,
        departs: null,
        arrives: null,
        actualDepart: null,
        actualArrive: null
    };

    this.fill = function (info) {
            for(var prop in this.data) {
        if(this.data[prop] !== 'undefined') {
            this.data[prop] = info[prop];
        }
    }
    };

        this.triggerDepart = function () {
            this.data.actualDepart = Date.now();
        };
        this.triggerArrive = function () {
            this.data.actualArrive = Date.now();
        };
        this.getInformation = function () {
            return this.data;
        };
};  

exports.create = function (info) {
    var instance = new Flight();    
    instance.fill(info);

    count++;
    if(destinations.indexOf(info['destination']) <0) {
        destinations.push(info['destination']);
    }
    return instance;
};

exports.getCount = function() {
    return count;
};

exports.getDestinations = function () {
    return destinations;
};

路线/index.js

/*
 * GET home page.
 */

 var flight= require('../node_modules/flight');
 var flight1 = flight({
     number :1,
     origin: 'LAX',
     destination : 'DCA',
     departs: '9AM',
     arrives:'4PM'
 });

 var flight2 = flight({
     number : 2,
     origin: 'LAX',
     destination : 'PDX',
     departs: '10AM',
     arrives: '12PM'
 });

exports.flight1 = function(req, res){
  res.json(flight1.getInformation());
};

exports.flight2 = function(req, res){
  res.json(flight2.getInformation());
};

app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' === app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);

app.get('/flight1', routes.Flight1);

app.get('/flight2', routes.Flight2);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

不知道如何解决。

喝水

使用flight.create代替flight()

 var flight= require('../node_modules/flight');
 var flight1 = flight.create({
     number :1,
     origin: 'LAX',
     destination : 'DCA',
     departs: '9AM',
     arrives:'4PM'
 });

 var flight2 = flight.create({
     number : 2,
     origin: 'LAX',
     destination : 'PDX',
     departs: '10AM',
     arrives: '12PM'
 });

您可以通过简单地导出第一个模块来减轻问题,如下所示:

exports = function (info) {
    var instance = new Flight();    
    instance.fill(info);

    count++;
    if(destinations.indexOf(info['destination']) <0) {
        destinations.push(info['destination']);
    }
    return instance;
};

exports.getCount = function() {
    return count;
};

exports.getDestinations = function () {
    return destinations;
};

然后,你可以都使用flight(),你可以使用.create上面,但你也可以使用flight.getCountflight.getDestinations太,没有一个实例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Node.js:TypeError:对象不是函数

来自分类Dev

TypeError:对象不是Node JS / socket.io中的函数

来自分类Dev

TypeError:undefined不是Node JS函数

来自分类Dev

typeerror undefined 不是 node js 中的函数

来自分类Dev

“ TypeError:对象不是函数”

来自分类Dev

Node.js:对象不是函数

来自分类Dev

TypeError:对象不是将MongoDB与Node.js一起使用的函数

来自分类Dev

TypeError:对象不是函数-jQuery

来自分类Dev

TypeError:对象不是函数+ nodeJS

来自分类Dev

TypeError:对象不是函数-jQuery

来自分类Dev

JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

来自分类Dev

JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

来自分类Dev

猫鼬和节点js-TypeError:对象不是函数

来自分类Dev

Node.js异步并行“ TypeError:任务不是函数”

来自分类Dev

TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数

来自分类Dev

TypeError:不是Node和Express JS的构造函数

来自分类Dev

TypeError:回调不是Node.js中的函数

来自分类Dev

Node.js TypeError:未定义不是函数

来自分类Dev

TypeError:undefined不是Node.js中的函数

来自分类Dev

TypeError:undefined不是Node.js中管道的函数

来自分类Dev

TypeError:count 不是 node.js 和 sequelize 中的函数

来自分类Dev

JS:您的函数不是函数!TypeError

来自分类Dev

Node.js-不是函数

来自分类Dev

Node.js-不是函数

来自分类Dev

TypeError:对象不是在Express中显示的函数

来自分类Dev

角$超时-TypeError:对象不是函数

来自分类Dev

Javascript“未捕获的TypeError:对象不是函数”

来自分类Dev

AngularJS $ parse TypeError:对象不是函数

来自分类Dev

未捕获的TypeError:对象(...)不是函数

Related 相关文章

  1. 1

    Node.js:TypeError:对象不是函数

  2. 2

    TypeError:对象不是Node JS / socket.io中的函数

  3. 3

    TypeError:undefined不是Node JS函数

  4. 4

    typeerror undefined 不是 node js 中的函数

  5. 5

    “ TypeError:对象不是函数”

  6. 6

    Node.js:对象不是函数

  7. 7

    TypeError:对象不是将MongoDB与Node.js一起使用的函数

  8. 8

    TypeError:对象不是函数-jQuery

  9. 9

    TypeError:对象不是函数+ nodeJS

  10. 10

    TypeError:对象不是函数-jQuery

  11. 11

    JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

  12. 12

    JS:未捕获的TypeError:对象[object Object]的属性'$'不是函数

  13. 13

    猫鼬和节点js-TypeError:对象不是函数

  14. 14

    Node.js异步并行“ TypeError:任务不是函数”

  15. 15

    TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数

  16. 16

    TypeError:不是Node和Express JS的构造函数

  17. 17

    TypeError:回调不是Node.js中的函数

  18. 18

    Node.js TypeError:未定义不是函数

  19. 19

    TypeError:undefined不是Node.js中的函数

  20. 20

    TypeError:undefined不是Node.js中管道的函数

  21. 21

    TypeError:count 不是 node.js 和 sequelize 中的函数

  22. 22

    JS:您的函数不是函数!TypeError

  23. 23

    Node.js-不是函数

  24. 24

    Node.js-不是函数

  25. 25

    TypeError:对象不是在Express中显示的函数

  26. 26

    角$超时-TypeError:对象不是函数

  27. 27

    Javascript“未捕获的TypeError:对象不是函数”

  28. 28

    AngularJS $ parse TypeError:对象不是函数

  29. 29

    未捕获的TypeError:对象(...)不是函数

热门标签

归档