通过nodejs中的函数清理将数据插入mongodb的方法?

时间:2016-12-29 15:49:24

标签: node.js mongodb

我是nodejs的新手,我有这个丑陋的代码,我觉得应该在自己的函数中,但我不知道如何创建函数。在我的post方法中,我有这么长的代码从身体请求数据并将其存储到我的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");
                }
            });
});

是否可以将此数据请求放入其自己的函数中,并从post方法中调用它?如何实现?

我的俱乐部架构

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
}

});

1 个答案:

答案 0 :(得分:2)

基于MVC架构,控制器可用于数据与客户端请求之间的通信。所以你可以定义你的http动词的所有回调函数,包括控制器中的get,post,delete,....

<强>路由/ router.js

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

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

<强>控制器/ 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");
            }
        });
    }
};