This is an old question that has been asked many times. I have tried all the solutions but nothing works.
I am using MongDB and want the names of users stored in the database to be displayed on the main page as a list even after closing and reopening the browser. When I import phones.js in code.js, I get :
ReferenceError: require is not defined
Thus, I can't use the database to display the names. I understand that browsers don't have the require method defined, Node.js does. But is there a turnaround to this?
app.js is in main directory ( . / )
const mongoose = require('mongoose');
const Phones = require('./models/phones');
const { ObjectId } = require('mongodb');
//...MongoDB connection
app.use(express.static(path.join(__dirname, 'public')));
//...creating endpoints
phones.js is in ./models/phones.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//...create Schema
const Phones = mongoose.model('Phones', phonesSchema);
module.exports = Phones;
code.js in ./public/code.js
const Phones = require('../models/phones.js');
console.log(Phones.find({}));
//...create an 'li' for each number and append it to 'ul'
index.html in ./public/index.html
<script src="code.js" ></script>
I have used browserify : browserify app.js -r bundle.js and I get a huge error.
If there's another approach to display on a page the names stored in the database even after closing the browser it will be accepted as the solution also.