Javascript Node.js服务器瀑布错误类型错误:无法读取属性';Symbol(Symbol.toStringTag)和#x27;未定义的
我正在编写一个具有两个函数的服务器,一个使用另一个函数的输出。服务器运行时,会出现错误:Javascript Node.js服务器瀑布错误类型错误:无法读取属性';Symbol(Symbol.toStringTag)和#x27;未定义的,javascript,node.js,express,asynchronous,waterfall,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
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.瀑布([yelpSearch(输入[0]),googleSearch]
。我正在使用Yelp API搜索餐厅,然后对于每个餐厅,我想获取该餐厅图像的Google搜索。yelpSearch和googleSearch
都是常量;它们不会被提升。因此,在调用异步瀑布([yelpSearch(输入[0]),googleSearch时,
,函数还不存在,因此失败。请在路由器上方声明它们。改为获取(或将它们更改为已挂起的函数声明)。我猜,您将参数错误地传递给瀑布中的第一个函数,应该是:
async.waterfall([
async.constant(input[0]),
yelpSearch,
googleSearch
], function sendJson(err, restaurants) {
// ...
});
该错误发生在代码的哪一行?是否缺少异步的导入?@CertainPerformance:我在问题中编辑了它。它发生在async.瀑布()上@Gerik:不,我没有,它是说异步没有定义,它没有定义的原因是因为它没有被正确导入、创建或引用。你能告诉我们它是如何被导入/包含的吗?我在上面的代码中没有看到它。它不起作用。而且,我认为它在yelpSeach()中失败了你知道为什么我不能直接将输入解析到函数中吗?