简体   繁体   中英

Module.exports with constructor and other objects

I'm trying to do something like this:

let Token = mongoose.model("Token", InToken)

let add = () => {
    return 1 + 1
}

module.exports = {
    Token,
    add
}

Whenever I do this, I receive this error: TypeError: Token is not a constructor .

How can I export the model along with other objects?

When you import your module with

const Something = require('./Something'); // contains the file in your question

you will be able to use new with:

const token = new Something.Token()

您应该这样导入令牌:

const { Token } = require("path to token model");

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