Clean way of inserting data into mongodb through a function in your nodejs?

Recap

Im new to nodejs and I have this ugly piece of code that I feel should be in its own function but I dont know how to create the function. In my post method I have this long piece of code requesting data from the body and storing it into my mongodb.

router.post("/club-affiliation-registration", function (req, res) {
var club = {
    clubName: req.body.clubName,
    clubAddress: req.body.clubAddress,
    clubDisciplines: req.body.clubDisciplines,
    clubEmail: req.body.clubEmail,
    clubWebsite: req.body.clubWebsite,
}

var clubChairperson = {
    firstName: req.body.chairpersonFirstName,
    secondName: req.body.chairpersonLastName,
    phone: req.body.chairpersonPhone,
    email: req.body.chairpersonEmail
}

var clubSecretary = {
    firstName: req.body.secretaryFirstName,
    secondName: req.body.secretaryLastName,
    phone: req.body.secretaryPhone,
    email: req.body.secretaryEmail
}

var clubTreasurer = {
    firstName: req.body.treasurerFirstName,
    secondName: req.body.treasurerLastName,
    phone: req.body.treasurerPhone,
    email: req.body.treasurerEmail
}
var clubChildProtectionOfficer = {
    fullName: req.body.childProtectionOfficerName,
    phone: req.body.childProtectionOfficerMobile,
    email: req.body.childProtectionOfficerEmail
}
var meta = {
    clubPaymentId: result.transaction.id
}
// storing in database
var newClub = {
                club: club,
                clubChairperson: clubChairperson,
                clubSecretary: clubSecretary,
                clubTreasurer: clubTreasurer,
                clubChildProtectionOfficer: clubChildProtectionOfficer,
                meta: meta
            }
Club.create(newClub, function (error, newlyCreatedClub) {
                if (error) {
                    console.log(error);
                } else {
                    req.flash("success", "You Application has been submitted. Please save your payment number: " + result.transaction.id);
                    res.redirect("/about");
                }
            });
});

Is it possible to put this data im requesting into its own function and just call it from the post method? How would that be achieved?

My club schema

var ClubSchema = new mongoose.Schema({
// first page
club: {
    clubName: String,
    clubAddress: String,
    clubDisciplines: String,
    clubEmail: String,
    clubWebsite: String,
    clubSponsor: String
},
// second page
clubChairperson: {
    firstName: String,
    secondName: String,
    phone: String,
    email: String
},
clubSecretary: {
    firstName: String,
    secondName: String,
    phone: String,
    email: String
},
clubTreasurer: {
    firstName: String,
    secondName: String,
    phone: String,
    email: String
},
clubChildProtectionOfficer: {
    fullName: String,
    phone: String,
    email: String
},
meta:{
    clubSubmission : { type : Date, default: Date.now },
    clubPaymentId: String
}

});
dNitro

Based on MVC architecture, Controllers are resposible for communications between data and your client requests. so you could define all your callback functions to your http verbs include get, post, delete, ... in a controller.

routes/router.js

var controller = require('../controllers/controller.js');

router
    .route("/club-affiliation-registration")
        .get(controller.getRegistration)
        .post(controller.postRegistration);

controllers/controller.js

var Club = require('../models/club.js');

module.exports = {

    getRegistration: function(req, res) {},

    postRegistration: function(req, res) {
        var club = {
            clubName: req.body.clubName,
            clubAddress: req.body.clubAddress,
            clubDisciplines: req.body.clubDisciplines,
            clubEmail: req.body.clubEmail,
            clubWebsite: req.body.clubWebsite,
        };

        var clubChairperson = {
            firstName: req.body.chairpersonFirstName,
            secondName: req.body.chairpersonLastName,
            phone: req.body.chairpersonPhone,
            email: req.body.chairpersonEmail
        };

        var clubSecretary = {
            firstName: req.body.secretaryFirstName,
            secondName: req.body.secretaryLastName,
            phone: req.body.secretaryPhone,
            email: req.body.secretaryEmail
        };

        var clubTreasurer = {
            firstName: req.body.treasurerFirstName,
            secondName: req.body.treasurerLastName,
            phone: req.body.treasurerPhone,
            email: req.body.treasurerEmail
        };

        var clubChildProtectionOfficer = {
            fullName: req.body.childProtectionOfficerName,
            phone: req.body.childProtectionOfficerMobile,
            email: req.body.childProtectionOfficerEmail
        };

        var meta = {
            clubPaymentId: result.transaction.id
        };

        // storing in database
        var newClub = {
            club: club,
            clubChairperson: clubChairperson,
            clubSecretary: clubSecretary,
            clubTreasurer: clubTreasurer,
            clubChildProtectionOfficer: clubChildProtectionOfficer,
            meta: meta
        };

        Club.create(newClub, function(error, newlyCreatedClub) {
            if (error) {
                console.log(error);
            } else {
                req.flash("success", "You Application has been submitted. Please save your payment number: " + result.transaction.id);
                res.redirect("/about");
            }
        });
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related