简体   繁体   中英

Defining a class in Node.js

I am trying to make a model class in Node.js and am having some trouble.


Code:

MosaicStreamer.js

This is how I'm trying to define the class.

function MosaicStreamer() {

}

module.exports = MosaicStreamer; 

App.js

app.get('/map', function(req, res){
    'use strict'; 
    var models = require('./models/MosaicStreamer.js'); 
    var mosaic_streamer = new MosaicStreamer; 
    res.render('view', {type: "block", name: "Sara", latitude: "", longitude: "", zoom: ""}); 
}); 

Here I am trying to create an instance of the MosaicStreamer class.


Error:

I keep getting:

ReferenceError: MosaicStreamer is not defined

The stack trace points to var mosaic_streamer = new MosaicStreamer; in app.js .

What am I doing wrong?

Every function and variable has to be defined in each file separatly.

Simply replace

var models = require('./models/MosaicStreamer.js');

with

var MosaicStreamer = require('./models/MosaicStreamer.js');

to assign the exported named function MosaicStreamer (class) to your local variable MosaicStreamer instead of models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM