I’m trying to pass data from my server app.js to my database file
Relevant server code app.js
const base = require('./base.js');
app.post('/',(req, res)=>{
var userName = req.body;
base.getUserName(userName);
res.render('index');
});
Relevant database code
var userDataFinal = function getUserName(user){
return user;
}
var exports = module.exports = {getUserName};
console.log(userDataFinal)
This gives me a reference error: getUserName is not defined,
however if I keep the console.log() within the scope of the getUserName function the code runs fine. I need to be able to access the data in user outside the scope of the getUserData function and it doesnt seem to be working
I made the changes however I’m getting an unhandled promise rejection warning now
var userDataFinal = function getUserName(user){
return user;
}
module.exports = {userDataFinal};
////////////////////////////////////////////////////////////////////////
// connects to the database
MongoClient.connect(URL, {useNewUrlParser: true}, (error, client)=>{
if(error){
return console.log('Could not connect to the database');
}
// creates a new collection
const db = client.db(database);
// adds a document to the designated collection
db.collection('User').insertOne(userDataFinal);
console.log('Database is connected...')
});