TypeError:object不是Node.js函数 [英] TypeError: object is not a function Node.js

查看:100
本文介绍了TypeError:object不是Node.js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Lynda.com上进行Node.js Essential Training ...

I'm doing the Node.js Essential Training on Lynda.com...

关注视频但我在终端中收到此错误。

Following the video but I get this error in the terminal.


TypeError:object is not function

"TypeError: object is not a function"

node_modules / flight / index.js

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

routes / index.js

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

您可以通过简单导出第一个模块来缓解此问题,如下所示:

You could mitigate the issue by simple exporting the first module like below:

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.getCount 航班.getDestinations 也是,没有实例。

Then you could both use flight() as you could use .create above, but you can also use flight.getCount and flight.getDestinations too, without an instance.

这篇关于TypeError:object不是Node.js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆