2017-09-15 46 views
2

我在的NodeJS定義了兩個簡單的功能函數組成:的ReferenceError:不定義的錯誤,而試圖使用

function findEntityDetailsAsIs(modelname, callback) { 
    modelname.find({}, function(error, result) { 
     if (error) 
      callback (error, null); 
     else { 
      //console.log(result); 
      callback (null, result); 
     } 
    }); 
}; 

這是我的第一個功能,另一個功能是

function printEntitydetails(error, entitydetails, callback) { 
    console.log(entitydetails); 
} 

我試圖將這些功能稱爲

findEntityDetailsAsIs(fieldLabel, printEntitydetails(error, entitydetails)); 

但是,當我嘗試運行此函數調用它拋出一個錯誤

ReferenceError: error is not defined 

但錯誤只是作爲我從回調傳遞的佔位符對象。

findEntityDetailsAsIs(fieldLabel, printEntitydetails(entitydetails)); 

我試過在通話中跳過錯誤,但這次它給出了這個錯誤。

ReferenceError: entitydetails not is not defined 

按我的知識findEntityDetailsAsIs應該提供entitydetails的背景下,爲我提供了一個callback(null, result)

+0

當你在函數名後面添加圓括號時,它會調用它並將其輸出作爲回調函數傳遞。嘗試'printEntitydetails.bind(null,error,entitydetails)' – Rajesh

回答

3

你的函數findEntityDetailsAsIs期望獲得回調函數,而不是它的執行結果。
你只需要提供的函數名稱:

findEntityDetailsAsIs(fieldLabel, printEntitydetails); 

當你運行像你一樣,你傳遞給findEntityDetailsAsIs結果printEntitydetails,而不是函數本身。因爲該函數什麼也沒有返回,所以你得到undefined

+0

OP得到* ReferenceError:錯誤未定義*。即使我懷疑相同的問題,但錯誤信息說,否則 – Rajesh

+1

答案應該工作。 如果您仍然收到錯誤,意味着其他錯誤我不確定,但可能是版本問題 – abdulbarik

+0

嘿,感謝您的幫助。 – dprophecyguy

相關問題