Object attribute can be filled with function call? Javascript

Asked

Viewed 30 times

0

I’m trying to turn a file. js in module so I can access it in another .js. So this file I want to export needs to be it integer an object, right?

Example: File "database"

module.exports = {
        mongoose: require('mongoose')
        conecta: async function conectarDb() {
                const uri = 'mongodb://localhost:27017/covid-database'
                await mongoose.connect(uri, { useUnifiedTopology: true, useNewUrlParser: true })
            /*...*/
         }
}

The question is this: Does it work that I fill an object property with a function call (Mongoose: require('Mongoose')? In my case, by giving "Mongoose.conect(...)" in the function of the connecting variable defined below, it will use the Mongoose attribute that I set just above?

  • 1

    Is there any particular reason for it? this.mongoose in function may be what you need, although I do not see much use.

  • That solved it. It’s just that I have two files: server.js and bd.js (the one with the above). I’m using express on server.js. What I want to do is use bd.js as a module in server.js, so I can pick up objects received from the front end by express, and save them in the database. I’m just really confused. That’s why I use Mongoosis. But I’m confused, I don’t know how to deal with asynchronicity in all this.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.