Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: cognitiveServices.face is not a constructor

I am using Microsoft Cognitive Services api for nodejs. I have following code

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);
});

however, when I execute this code I get following error

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

How can I resolve this error?

like image 712
Om3ga Avatar asked Sep 22 '16 13:09

Om3ga


1 Answers

It looks like the documentation for the cognitive-services module is incorrect: you need to call cognitiveServices.face(...) without new.

If you look at https://github.com/joshbalfour/node-cognitive-services/blob/master/api/face.js, you can see that face is defined as an arrow function, which makes it not a constructor. See https://stackoverflow.com/a/37037600/483595 for details on why that is the case.

Edit: looks like the issue is already reported here: https://github.com/joshbalfour/node-cognitive-services/issues/2

like image 189
ArthurDenture Avatar answered Oct 18 '22 09:10

ArthurDenture