In a separate file, there is a function that checks for the existence of an admin in the database. If no admin is found, it creates one. The function should return true if an admin is present by the end, and false if not.
/*AdminUser.js*/
const Admin = require('../models/admin');
const mongoose = require('mongoose');
module.exports = {
validate: function() {
Admin.getAdminByRealID("1212", (error, admin) => {
if (admin) {
console.log("Main admin is Booted");
return true;
} else {
console.log("Booting main admin");
let newAdmin = new Admin({
ID: "1212",
account: {
username: "Admin",
password: "1212",
token: "Admin"
},
name: {
first: "Admin",
last: "Delta"
},
communication: {
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3357565f475273564b525e435f561d-505e">[email protected]</a>",
phone: "1212"
}
});
Admin.addAdmin(newAdmin, (error, admin) => {
if (error) {
console.log("Admin reboot failed");
return false;
} else {
console.log("Admin have been initialized");
return true;
}
});
}
});
}
}
In the main application, the following code is used:
if (AdminUser.validate()) {
app.listen(port, () => {
console.log('Server started on port: ' + port);
});
} else {
console.log('Fail to run Server');
process.exit();
}
The .validate function is returning an 'undefined' object causing the server to fail to start. Can someone clarify why the return value is not true or false?
The .validate function does print "Main admin is booted" so I assume it returns true afterwards.
Using app = express();