简体   繁体   中英

Typerror is not a constructor

I´ve this error on my code

Typerror: ProjectRepository is not a constructor at main.....

This is the code from main.js

const Promise = require('bluebird')
const AppDAO = require('./dao')
const ProjectRepository = require('./project_repository')
const TaskRepository = require('./task_repository')

function main() {

const dao = new AppDAO('./database.sqlite3')
const blogProjectData = {name: 'Write Node.js - SQLite Tutorial'}
const projectRepo = new ProjectRepository(dao)
const taskRepo = new TaskRepository(dao)
let projectId

This is project_repository.js class with the constructor

class ProjectRepository{
    constructor(dao){
        this.dao = dao
    }

It's very simple but I´ve no idea why it´s failing.

Because you are not export ing the ProjectRepository class.

add export before class keyword.

export class ProjectRepository{ ... }

Maybe it's just a silly mistake, but if you want to learn more about Javascript import and exports you can at this link

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