2015-06-09 35 views
1

我有一些麻煩與一個.js對象的屬性,當我問我沒有更新。類屬性沒有更新[node.js - express]

我是新來的JavaScript世界,所以我希望我的問題不會太棘手。

首先,這是我的節點類的一部分:

Node = function (label, vals, db, callback) { 
    // attributes 
    this.label = label; 
    this.vals = vals; 
    this.db = db; 
    if (typeof calback == 'function') calback(); 
} 
Node.prototype.insert_db = function (calback) { 
    var vals = this.vals; 
    // Create a node 
    console.log("1:"); // output print 
    console.log(vals); // output print 
    this.db.save(vals, function (err, node) { 
     if (err) throw err; 
     vals = node; 
     console.log("2:"); // output print 
     console.log(vals); // output print 
    }); 
    this.vals = vals; 
    console.log("3:"); // output print 
    console.log(this.vals); // output print 
    if (typeof calback == 'function') calback(); 
} 

我想要使用此代碼做的,是更新插入後,我的Node對象的「ID」值。但是...這裏是乳寧這個代碼後我的console.log:

var per = new Node('Whatever', { firstName: "aaaa", lastName: "aaaa", age: "aaaa" }, db2); 
per.insert_db(); 

控制檯輸出:

1: 
{ firstName: 'aaaa', lastName: 'aaaa', age: 'aaaa' } 
3: 
{ firstName: 'aaaa', lastName: 'aaaa', age: 'aaaa' } 
2: 
{ firstName: 'aaaa', lastName: 'aaaa', age: 'aaaa', id: 491 } 

第三狀態(在第二位置)永遠不會更新。 我不知道它來自哪裏。我已經在這個問題上度過了最後2天,我顯然無法處理了。

在此先感謝。

編輯 仍有麻煩。 感謝@MarkHughes,這裏是我的新代碼

Node.prototype.insert_db = function (callback) { 
    var temp = this; 
    this.db.save(this.vals, function (err, node) { 
     if (err) throw err; 
     temp.vals = node; 
     console.log(temp.vals); // output print 
     if (typeof callback == 'function') callback(); 
    }); 
} 

代碼跑這個一個

var per = new Node('Person', { firstName: "aaaa", lastName: "aaaa", age: "aaaa" }, db2); 
per.insert_db(res.render('login-index', { title: 'miam', dump: mf.dump(per) })); 

現在,這裏是console.log(temp.vals)

{ firstName: 'aaaa', lastName: 'aaaa', age: 'aaaa', id: 515 } 

,這裏是渲染輸出(預html):

vals: object(3): { 
    firstName: string(4): "aaaa" 
    lastName: string(4): "aaaa" 
    age: string(4): "aaaa" 
} 

輸出是回調函數是的異步函數.save ...而且還沒有更新。 :(

我不知道這是否會有所幫助,只有調試:

setTimeout(function() { console.log(temp) }, 3000); 

.save功能之後寫入返回此:

[...] 
vals: { firstName: 'aaaa', lastName: 'aaaa', age: 'aaaa', id: 517 }, 
[...] 

我在做什麼錯

回答

1

.save是異步的,因此您不能使用它在回調之外返回的值,因此將其餘的代碼移到回調中可以修復它:

var t = this; 
this.db.save(vals, function (err, node) { 
    if (err) throw err; 
    vals = node; 
    console.log("2:"); // output print 
    console.log(vals); // output print 

    t.vals = vals; 
    console.log("3:"); // output print 
    console.log(t.vals); // output print 
    if (typeof calback == 'function') calback(); 
}); 

將「this」保存到變量中允許從回調中對其進行訪問。

對於您的調用代碼,您需要確保您在insert_db()的回調中呈現輸出 - 例如,是這樣的:

per.insert_db(function() { 
    res.render('login-index', { title: 'miam', dump: mf.dump(per) }) 
}); 

注意,您必須在一個函數(別的)什麼會回調將執行該功能,而不是函數本身的返回值被傳遞到包裹res.render。

+0

這裏的問題是'this.db.save'函數中的'this.vals'不引用我的Node對象,而是引用不同的全局對象。 –

+0

@JohnSmith好的我已經更新了 - 這是關於Javascript中的變量作用域規則,可能有點令人困惑:)通過將'this'保存到本地變量中,然後可以從回調中更新它。 –

+0

@JohnSmith還要注意調用''calback()'**在這個回調中的重要性,所以它不會執行,直到保存完成後 - 因此調用insert_db的任何東西都可以讓代碼等待保存在執行之前完成。 –