TypeError: cognitiveServices.Face不是构造函数

TypeError: cognitiveServices.face is not a constructor

本文关键字:构造函数 Face cognitiveServices TypeError      更新时间:2023-09-26

我正在为nodejs使用微软认知服务api。我有以下代码

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

如何解决这个错误?

看起来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