1

この投稿の一番下までスクロールして、回避策/可能な解決策を確認してください。

これは、ソース コードでコメントを付けて説明した方が簡単かもしれません。当面の問題は、実行しようとしているタスクを実行するために疑似クラスがどのように連携するかを理解できないことです (以下のコードで説明されています)。

コードは、lead.js、router.js、および db.js の 3 つのファイルに分割されます。

かなりの量のコード行がありますが、そのほとんどはコメントです。

[lead.js]

var bcrypt = require('bcrypt'),
    validators = require('../lib/validators'),
    utility = require('../lib/utility'),
    document = {};

var Lead = module.exports = function (db) {
  // Save a reference to the database.
  this.db = db;

  // Reference initial document.
  // This is totally wrong, not sure how to 'send' a variable to the constructor of a class
  // when I cannot add another param. Due to how I'm importing the db model, I won't know what
  // the document is until I fill out the form. I've also tried 'document' instead of 'Lead.document'.
  this.document = Lead.document;

  // Setup the document if it exists.
  // This also doesn't work.
  // Basically I want to be able to set up a document variable outside of this module (line #100),
  // Then pass it to this module after filling it up with values from a form.
  // Then based on what's been filled in, it would fix up (trim, convert to lower case)
  // some of the values automatically and default a few values that I'm not always going to pass.
  if (!document) {
    var salt = bcrypt.genSaltSync(10),
        hash = bcrypt.hashSync(utility.generatePassword(), salt);

    // Default values.
    if (!document.meta.createdAt) { this.document.meta.createdAt = Date.now(); }
    if (!document.login.password) { this.document.login.password = hash; }
    if (!document.login.role) { this.document.login.role = 'User'; }

    // Normalize a few values.
    this.document.login.email = document.login.email.toLowerCase().trim();
    this.document.contact.name.first = document.contact.name.first.trim();
    this.document.contact.name.last = document.contact.name.last.trim();
    this.document.contact.address.street = document.contact.address.street.trim();
    this.document.contact.address.city = document.contact.address.city.trim();
    this.document.contact.address.state = document.contact.address.state.trim();
    this.document.contact.address.zip = document.contact.address.zip.trim();
    this.document.contact.phone.home = document.contact.phone.home.trim();
  }
  // So in regards to the above code, the end result I'm looking for is...
  // I want to append some properties to the this.document reference when the document is empty (when I'm updating it, I won't set the document), 
  // and on new documents it will append a few default values/normalize all the fields.
};

Lead.prototype.validate = function(fn) {
  var errors = [];

  // Some validation rules I cut out to make this shorter.

  if (errors.length) return fn(errors);
  fn();
};

Lead.prototype.save = function(fn) {
  this.db.collection('leads', function(err, collection) {
    if (err) { fn(new Error({message: err})); }

    collection.insert(this.document, function(err, result) {
      return fn(err, result);
    });
  });
};

---

[route.js file]

  var db = require('../models/db');

  app.post('/register', function(req, res) {
    var data = req.body.lead || {};

    // Fill the document.
    var document = {
      meta: {
        host: req.headers.host,
        referer: req.headers.referer,
        createdIPAddress: req.connection.remoteAddress
      },
      login: {
        email: data.email
      },
      contact: {
        name: {
          first: data.first,
          last: data.last
        },
        address: {
          street: data.street,
          city: data.city,
          state: data.state,
          zip: data.zip
        },
        phone: {
          home: data.phone
        }
      }
    };

    // Write the document.
    db.lead.document = document;

    db.lead.validate(function(err) {
      if (err) {
        req.session.error = err;
        return res.redirect('back');
      }

      db.lead.save(function(err) {
        res.redirect('/register/success');
      });
    });
  });

---
[db.js]

var mongodb = require('mongodb'),
    server = new mongodb.Server('localhost', 27017),
    connection = new mongodb.Db('test', server);

connection.open(function(err, db) {});

module.exports =  {
  lead: new (require('./lead'))(connection)
};

これを実行すると、バリデーターは常にパスワードが空であると報告しますが、これは理にかなっています。最初に空のパスワードを使用してドキュメントをクラスに送信しています (パスワードはランダムに生成され、フォーム フィールドではありません)。問題は、if (!document) ... コード ブロックをどう処理すればよいかわかりません。 this.document を実際に適切に設定します。

コメントとコードの間で、私が何をしようとしているのかを理解していただければ幸いです。私はしばらくこれにこだわっています。

編集

解決策を得るために、その流れを少し変更しました。

db.js では、リード (および将来のモデル) を直接インスタンス化するのではなく、接続をエクスポートしました。

router.js ファイルでは、db と Lead ファイルを要求し、Lead のコンストラクターで db 接続とドキュメントの両方を渡します。元。

var lead = new Lead(db, document);

lead.js ファイルでは、this.document = document (db と同じ) を実行するだけで簡単になります。新しいリードを送信すると、router.js から送信していない値 (作成日、ランダムなパスワードなど) がドキュメントに追加され、すべて問題ありません。

これはこれを処理する適切な方法ですか、それともこれをリファクタリングするためのより良い方法はありますか?

4

3 に答える 3

1

このコードを思い通りに動作させたとしても、これは完全に間違った方法です。この例では、シングルトン リードがあります。/register url をリクエストすることにより、'document' フィールドをこの singleton に設定します。(重要) ただし、リクエストは非同期で機能します。検証したばかりのドキュメントを保存するという保証はまったくありません。新しいリクエストがリード オブジェクトに上書きされる可能性があるためです。このロジックは、リクエスト スコープで実行する必要があります。1 つのリクエストに対して 1 つのスコープ。すべての人に1つではありません。

于 2012-03-16T00:28:40.513 に答える
0

あなたがvar document = {}初期設定し、偽りで{}はありません。必要なプロパティを割り当てる前に、開始値として設定し、設定を確認した後の方がよいでしょうdocument = null!documentdocument = {}

于 2012-03-16T00:21:07.283 に答える
0

Javascript でのオブジェクト指向プログラミングについて読む必要があります。

コードの上部近くで定義している無名関数documentコンストラクター関数であるため、現在初期化されていない必要なプロパティに関しては、次のように入力するだけです。

this.document = null;

その後、このコンストラクターを使用して新しいオブジェクトを作成すると、次のようになります。

var myLead = new Lead(dbConnection);

あなたはmyLead.document財産を持っているでしょう。

ただし、コードには他にも多くの問題があります。documentとして定義されているときに、ライブラリに関連データが表示されるグローバル変数があると想定しているのはなぜ{}ですか? ifコンストラクターの最後にあるそのステートメントのコードは、documentプロパティが下の他のファイルに設定されているときに実行する必要があり、存在することのみを期待this.documentする必要があります。

于 2012-03-16T00:21:36.230 に答える