Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i get my mongo schema to export into a file then use it to insert data?

I am having troubles being able to insert data into my collection, im not even sure im doing it correctly so i apologize for the vauge request but maybe my code will help you see what my intention is. The gist of it is im trying to make a seperate file for my schema/collection and then call it from another file and insert data and call other functions etc.

file1.js file:

require('dotenv').config()
 const User = require('./assets/js/data')

const bodyParser = require("body-parser");
const mongoose = require('mongoose');


mongoose.connect(process.env.url, { useNewUrlParser: true })
  .then(() => {
    console.log('Connected to MongoDB server');
  })



  // 1. Import the express module
const express = require('express');

// 2. Create an instance of the express application
const app = express();
app.set('views', './static/html');
app.set('view engine', 'ejs');
app.use(express.static('assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 3. Define the HTTP request handlers
app.get('/', (req, res) => {
  res.render('main')
});

app.get('/login', (req, res) => {
  res.render('login')
});

app.post('/login', (req, res) => {
  console.log(req.body.number);

  
  
  
})



app.listen(3000, (err) => {
  console.log("running server on port")
  if (err) {
    return console.log(err);
  }
})

data.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const mongoose = require('mongoose');

const userData = new mongoose.Schema({
    phoneNumber: String,
})

const User = mongoose.model('User', userData);



module.exports(
    User,
)

>Solution :

This line has the error.

// error
module.exports(
    User,
)

module.exports is not a function.

module.exports = User

// or

module.exports = { User }

if you do the first one, then required should be like this,

 const User = require('./assets/js/data')

otherwise

 const { User } = require('./assets/js/data')

More about module.exports

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading