1

私は、mongodb db ストアに保存する javascript(node.js) の例に取り組んでいます。

コードのスニペットに出くわしましたが、チュートリアルの作成者が何をしようとしているのか、そしてその理由がよくわかりません。私の理解不足ですが、ここで何が起こっているのか知りたいです。問題の行の横に私のコメントがある以下のコードを参照してください。

  //save new employee
  EmployeeProvider.prototype.save = function(employees, callback) {
      this.getCollection(function(error, employee_collection) {
        if( error ) callback(error)
        else {

          // This is the portion I have a question on. 
          // So this is checking to see if the array.length is undefined? Why would the length property be undefined?
          if( typeof(employees.length)=="undefined")
            // What is going on here exactly? 
            // It looks like he is initializing and array with employees parameter that was passed in to the save 
            // function earlier? I've never seen this kind of thing done before. Thoughts?
            employees = [employees];

          for( var i =0;i< employees.length;i++ ) {
            employee = employees[i];
            employee.created_at = new Date();
          }

          employee_collection.insert(employees, function() {
            callback(null, employees);
          });
        }
      });
  };

助けてくれてありがとう!

クリス

4

2 に答える 2