2016-12-29 5 views
0

私はnodejsに新しく、私はこの醜いコードを持っています。私はそれ自身の機能にすべきだと感じていますが、その機能の作成方法はわかりません。私のポストメソッドでは、私はこの長いコードを身体からデータを要求し、それを私のmongodbに格納しています。nodejsの関数を使って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"); 
       } 
      }); 
}); 

このデータを独自の機能に入れて、ポストメソッドから呼び出すことは可能ですか?どのように達成されるでしょうか? MVCアーキテクチャに基づいて

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 
} 

}); 
+0

あなたは私達にあなたの 'Club'モデルスキーマの定義を表示することができますか?モデルの再設計では、現在のスキーマがDRYの原則に違反しているため、 'firstName'、' secondName'、 'phone'、' email'などの適切な情報を保持する別個のユーザーコレクションを作成することができます( 'D' on't 'R'epeat' Y'自身)。 – chridam

+0

@chridam私はちょうどそれを追加しました。 req.bodyのすべてを関数に入れてnewClubを返そうとしましたが、 '未定義のプロパティ' body 'を読み取ることができません。 – Recap

+0

@chridamありがとうございました。今私はそれを私のスキーマに適用しましたが、それは質問に関連していませんでした。 – Recap

答えて

2

マイクラブスキーマは、コントローラは、データとクライアントの要求との間の通信のためのresposibleです。あなたの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"); 
      } 
     }); 
    } 
}; 
+0

これは私が探していたものです。それは事をきれいにしました。コントローラーをもっと見なければならないので、私はそれをよりよく理解することができます。 – Recap