2016-09-22 152 views
7

我正在爲nodejs使用Microsoft Cognitive Services api。我有以下代碼TypeError:cognitiveServices.face不是構造函數

const cognitiveServices = require('cognitive-services'); 

    const face = new cognitiveServices.face({ 
     API_KEY: yourApiKey 
    }) 

    const parameters = { 
     returnFaceId: "true" 
     returnFaceLandmarks: "false" 
    }; 
    const body = { 
     "url": "URL of input image" 
    }; 


    face.detect({ 
      parameters, 
      body 
     }) 
     .then((response) => { 
      console.log('Got response', response); 
     }) 
     .catch((err) => { 
      console.error('Encountered error making request:', err); 
     }); 

然而,當我執行這個代碼,我得到以下錯誤

const face = new cognitiveServices.face({ 
      ^

    TypeError: cognitiveServices.face is not a constructor 
     at Object.<anonymous> (/Users/..../face.js:3:14) 
     at Module._compile (module.js:556:32) 
     at Object.Module._extensions..js (module.js:565:10) 
     at Module.load (module.js:473:32) 
     at tryModuleLoad (module.js:432:12) 
     at Function.Module._load (module.js:424:3) 
     at Module.runMain (module.js:590:10) 
     at run (bootstrap_node.js:394:7) 
     at startup (bootstrap_node.js:149:9) 
     at bootstrap_node.js:509:3 

我怎樣才能解決這個問題?

+0

你有那個模塊的頂部需要聲明,對不對?你可以編輯你的問題,包括該聲明?同樣,根據https://github.com/joshbalfour/node-cognitive-services#installation上的「安裝和入門」步驟驗證您是否正確安裝了認知服務api會很好。 – ArthurDenture

+0

是的,我有,我更新了我的問題。 – 2619

+0

嗨,我的答案是否適合你?我看到賞金仍然開放... – ArthurDenture

回答

5

看起來cognitive-services模塊的文檔不正確:您需要撥打cognitiveServices.face(...)而不使用new

如果你看看https://github.com/joshbalfour/node-cognitive-services/blob/master/api/face.js,你可以看到face被定義爲一個箭頭函數,這使得它不是一個構造函數。有關詳細情況,請參閱https://stackoverflow.com/a/37037600/483595

編輯:看起來像這個問題已經被報告如下:https://github.com/joshbalfour/node-cognitive-services/issues/2

+0

此錯誤已得到修復:) –