2016-09-22 150 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

此错误已得到修复:) –