回调不是函数错误 [Nodejs]

克恩·埃利奥特

我有以下代码

var getUrl = function(callback) {
  ref.once('value')
    .then(function(snap) {
      let article_url = snap.val();
      console.log(article_url);
      callback(null, article_url); // I dont want to throw an error, so I pass null for the error argument
    });
};

var getImage = function(err, data, callback) {
  //if (err) throw err; // Check for the error and throw if it exists.
  textapi.extract({
    url: data,
    best_image: true
  }, function(error, response) {
    if (error === null) {
      //console.log(response.image);
      if (response.image == null || response.image == "") {
        image = "https://www.wefornews.com/wp-content/uploads/2017/01/news-3.jpg"
      } else {
        image = response.image;
      }
    }
  });
  callback(null, image); // Otherwise proceed as usual.
};

var updateDatabase = function(err, data) {
  //if (err) throw err; // Check for the error and throw if it exists.
  refupdate.update ({
    "Article_Image_URL": data
  });
  console.log('success'); // Otherwise proceed as usual
};

getUrl(getImage);
getImage(updateDatabase);

但是我callback is not a function在线路上遇到错误callback(null, image);

问题是,如果我删除该updateDatabase功能,则不会出现任何错误。但是,当它在代码中时,我会收到上面提到的错误。有人可以告诉我是什么导致了这个错误?

埃里克·B

调用时getUrl(getImage),该getImage函数将作为callback参数传递getUrlgetUrl然后调用这个callback参数,传递两个参数。

callback(null, article_url)

getImage需要三个参数

var getImage = function(err, data, callback)

所以函数callback内部getImage是未定义的,当你尝试调用 undefined 作为函数时

callback(null, image)

...你得到你的callback is not a function错误。

解决方案

您需要嵌套回调,以便以正确的顺序调用函数,并使用正确的参数。

getUrl(function (err, article_url) {
  getImage(err, article_url, function (err, image) {
    updateDatabase(err, image);
  });
});

这也可以简化,因为您想updateDatabasegetImage完成后直接调用

getUrl(function (err, article_url) {
  getImage(err, article_url, updateDatabase);
});

此外,在您的 getImage 函数中,callback会在您的textapi.extract函数完成之前调用,这可能不是您想要的。

var getImage = function(err, data, callback) {
  textapi.extract({
    url: data,
    best_image: true
  }, function(error, response) {
    /* code */
  });
  callback(null, image); // This will get called immediately, without waiting for textapi.extract
};

var getImage = function(err, data, callback) {
  textapi.extract({
    url: data,
    best_image: true
  }, function(error, response) {
    /* code */
    callback(null, image); // Callback moved here, so it gets called after textapi.extract completes
  });
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从mongoDB和Nodejs获取数据:toArray不是函数错误

来自分类Dev

TypeError:回调不是nodeJS中带有asyncJS的函数

来自分类Dev

TypeError:回调不是nodeJS中带有asyncJS的函数

来自分类Dev

NodeJs 异步瀑布(回调方法不是函数)

来自分类Dev

回调不是使用 nodejs 进行异步操作的函数

来自分类Dev

Nodejs - 异步 - 并行 - 限制回调不是函数

来自分类Dev

节点async.js:回调不是函数错误?

来自分类Dev

如何解决“回调不是函数错误”?

来自分类Dev

了解 NodeJS 中的回调函数

来自分类Dev

nodejs 在回调函数中绑定对象

来自分类Dev

无法从 NodeJS 的回调函数返回数据

来自分类Dev

回调函数在 NodeJS 中是如何工作的?

来自分类Dev

回调,错误:不是函数

来自分类Dev

回调,错误:不是函数

来自分类Dev

findOneAndUpdate,回调必须是一个函数,得到了[object Object],NodeJs,猫鼬错误

来自分类Dev

NodeJS回调

来自分类Dev

Angular / Nodejs中的未刷新函数错误

来自分类Dev

函数内的nodejs回调函数->如何返回结果

来自分类Dev

JavaScript地理位置错误-回调函数错误

来自分类Dev

FIREBASE 警告:用户回调引发了异常。TypeError:req.next 不是带有 nodejs 的 firebase 中的函数

来自分类Dev

流星:如何捕获异步回调函数错误

来自分类Dev

用户定义的函数,在nodejs中具有回调

来自分类Dev

将NodeJS回调从导出的函数传递到视图

来自分类Dev

NodeJs 函数等待直到回调返回值

来自分类Dev

带参数和回调的 Nodejs 函数不起作用

来自分类Dev

回调中的 Nodejs Mysql 回调

来自分类Dev

nodeJS:forEach 循环不是函数,错误

来自分类Dev

Javascript(NodeJS)回调范围

来自分类Dev

NodeJS MongoDB回调地狱

Related 相关文章

热门标签

归档