javascript - Node.js 服务器 waterfall 错误 TypeError : Cannot read property 'Symbol(Symbol.toStringTag)' of undefined

标签 javascript node.js express asynchronous waterfall

我正在编写一个具有两个函数的服务器,一个函数使用另一个函数的输出。服务器运行时报错:

TypeError: Cannot read property 'Symbol(Symbol.toStringTag)' of undefined
    at isAsync (/Users/charles/Documents/Router/node_modules/async/dist/async.js:228:32)
    at wrapAsync (/Users/charles/Documents/Router/node_modules/async/dist/async.js:232:12)
    at nextTask (/Users/charles/Documents/Router/node_modules/async/dist/async.js:5308:20)
    at Object.waterfall (/Users/charles/Documents/Router/node_modules/async/dist/async.js:5320:5)
    at /Users/charles/Documents/Router/routes/yelp.js:46:15
    at /Users/charles/Documents/Router/node_modules/mongojs/lib/cursor.js:59:24
    at handleCallback (/Users/charles/Documents/Router/node_modules/mongojs/node_modules/mongodb/lib/utils.js:120:56)
    at /Users/charles/Documents/Router/node_modules/mongojs/node_modules/mongodb/lib/cursor.js:683:5
    at handleCallback (/Users/charles/Documents/Router/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:171:5)
    at setCursorDeadAndNotified (/Users/charles/Documents/Router/node_modules/mongojs/node_modules/mongodb-core/lib/cursor.js:505:3)

代码是

const express = require('express');
const router = express.Router();

const request = require('request-promise-lite');
const async = require('async');

router.get('/yelp', function(req, res, next) {
  db.input.find({}, {
      term: 1,
      location: 1,
      _id: 0
    })
    .limit(1).sort({
      $natural: -1
    }, function(err, input) {
      if (err) {
        res.send(err)
      }

      console.log(input);
      async.waterfall([yelpSearch(input[0]), googleSearch],
        function sendJson(err, restaurants) {
          console.log("waterfall starting");
          if (err) res.send(err);
          res.json(restaurants);
        })
    })
});

// Yelp API call
const yelpSearch = function(input, cb) {
  const client = yelp.client(apiKey);
  client.search(input)
    .then(response => {
      console.log(response.jsonBody.businesses);
      cb(null, response.jsonBody.businesses);
    })
    .catch(e => {
      console.log(e);
    });
}

// Google API call
const googleSearch = function(restaurants, cb) {
  console.log("google starts")
  var apiKey = google_apiKey;
  var cseKey = cseID;
  restaurants.forEach(function(restaurant) {
    var keyWord = restaurant.name + restaurant.city + restaurant.state;

    var googleURL = "https://www.googleapis.com/customsearch/v1?key=" + apiKey +
      "q=" + keyWord +
      "&searchType=image" +
      "&cx" + cseKey +
      "&count=5" +
      "&safe=medium";

    var imageURLs = [];
    request.get(googleURL, {
      json: true,
      headers: {
        'User-Agent': 'thaorell'
      }
    }).then(function(response) {
      response.items.forEach(function(item) {
        imageURLs.append(item.link)
      });

      restaurant.append(imageURLs);
      console.log(imageURLs);
    })
  })
  cb(null, restaurants)
};

有人有这方面的经验吗?错误在于:async.waterfall([yelpSearch(input[0]), googleSearch]。我正在使用 Yelp API 搜索餐厅,然后对于每个餐厅,我想获取Google 搜索该餐厅的图片。

最佳答案

我猜,您错误地将参数传递给 waterfall 中的第一个函数,它应该是:

async.waterfall([
    async.constant(input[0]),
    yelpSearch,
    googleSearch
], function sendJson(err, restaurants) {
    // ...
});

关于javascript - Node.js 服务器 waterfall 错误 TypeError : Cannot read property 'Symbol(Symbol.toStringTag)' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973919/

相关文章:

node.js - 如何使用 Express 中间件路由在 REST API 中进行错误处理

javascript - 使用express配置CORS并使用axios在前端发出请求

javascript - 我无法在 Express 服务器中加载静态 JS

javascript - npm 找不到模块 '../lib/npm.js'

node.js - 使用busboy(nodejs)上传多个文件并发出相关的响应对象

node.js - 从 react 组件调用 Node 模块

javascript - Angular/JS Express 应用程序在生产模式下处于无限路由循环中,在开发模式下运行良好

javascript - 如何使用 Angular 8 切换行

javascript - 开发 VSCode 扩展时获取当前事件文件的目录

javascript - 通过另一个数据属性作为标识符获取元素的数据属性