簡體   English   中英

TypeError:對象不是Node.js函數

[英]TypeError: object is not a function Node.js

我正在Lynda.com上進行Node.js基本培訓...

跟隨視頻,但我在終端中收到此錯誤。

“ TypeError:對象不是函數”

node_modules /航班/ 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;
};

然后,您可以.create上面的.create一樣使用flight() ,但是也可以不使用實例也使用flight.getCountflight.getDestinations

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM