This is one of the main reaons I prefer to write my code in Coffeescript. 
 You don't have to do the this/that dance thing.  You just use the => 
notation to define functions.

On Tuesday, August 13, 2013 1:34:57 AM UTC-7, bodo wrote:
>
> Thank you guy
>
> On Tuesday, August 13, 2013 2:46:55 AM UTC+2, Rick Waldron wrote:
>>
>> `this.collection` doesn't yet exist, so the `else` path in `saveData` is 
>> being taken, which is sent to `connectDB` which calls 
>>  `MongoClient.connect`, but does not bind `this` to the callback.
>>
>> This change should work:
>>
>> this.connectDB(this.saveData.bind(this));
>>
>>
>>
>>
>>
>> On Mon, Aug 12, 2013 at 7:22 PM, bodo <vuducb...@gmail.com> wrote:
>>
>>> I did a pretty code:
>>>
>>> var Square = function(id,order,color,state) {
>>>
>>>  this.id = id;
>>> this.order = order;
>>>  this.color = color;
>>> this.state = state;
>>>  this.connected = false;
>>>
>>> MongoClient.connect(config.mongoURL, (function(err, db) {
>>>  this.connected = true;
>>> this.db = db; //db
>>>  this.collection = db.collection("squares"); //collection
>>> }).bind(this));
>>> }
>>>
>>> Square.prototype.connectDB = function(callback){
>>>
>>> (function(that){
>>>  MongoClient.connect(config.mongoURL, function(err, db) {
>>>  console.log("connected");
>>>  this.connected = true;
>>>  this.db = db; //db
>>> this.collection = db.collection("squares"); //collection
>>>
>>> //callback(that);
>>> callback();
>>>
>>> });
>>>
>>> })(this);
>>> }
>>>
>>>
>>> Square.prototype.test = function(){
>>>  console.log("test");
>>> }
>>>
>>> //export data
>>> Square.prototype.exportData = function(){
>>>
>>> var rtArr = {
>>> id:this.id,
>>>  order:this.order,
>>> color:this.color,
>>> state:this.state
>>>  }; 
>>>
>>> return rtArr;
>>> }
>>>
>>> //save datas to mongo
>>> Square.prototype.saveData = function () {
>>>
>>> if (this.collection){
>>>
>>> *var doc = this.exportData();*
>>>  this.collection.insert(doc,function() {
>>> console.log('inserted');
>>>  });
>>>  }else{
>>> this.connectDB(this.saveData);
>>> }
>>> }
>>>
>>> var square = new Square(6,1,'red',1);
>>> square.saveData();
>>>
>>> When I run this code, that display:
>>>
>>> throw err
>>>                     ^
>>> TypeError: Object #<Object> has no method 'exportData'
>>>
>>> Why it can not understand *var doc = this.exportData(); ?*
>>> *
>>> *
>>> Someone can help me ? Thank you very much
>>>
>>> -- 
>>> -- 
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines: 
>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to nod...@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> nodejs+un...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>  
>>> --- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "nodejs" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to nodejs+un...@googlegroups.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>  
>>>  
>>>
>>
>>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to