![](/img/trans.png)
[英]I'm getting the error: ```TypeError: Object(…) is not a function``` in my react app- trying to use hooks for the first time
[英]I'm trying to add poster URLs to my neo4j movie database using JavaScript, but I'm getting this undefined object error all the time
我对 JavaScript 完全陌生,并且很难理解异步调用。 我是否需要嵌套另一个承诺对象来设置海报 URL?
async function getIds() {
const result = await session.run("MATCH (m :Movie) RETURN m.imdbId LIMIT 5");
const ids = [];
result.records.forEach(record => ids.push(record._fields[0]));
return ids;
}
async function imdbIds() {
const ids = await getIds();
console.log(ids);
const response = await Promise.all(ids.map(async id => {
// webscraper that returns poster url of each imdbid
const url = toString(scraper(id));
console.log(url);
// query to set poster of each id in neo4j db
const result = await session.run("MATCH (m :Movie {imdbId : $id}) SET m.poster = $url RETURN m.poster", { id, url });
result.records.forEach(record => console.log(record._fields[0]));
return result;
}));
console.log(response);
}
imdbIds();
控制台日志仍然没有运气:
[ '0114709', '0113497', '0113228', '0114885', '0113041' ]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[object Undefined]
[ { records: [ [Record] ],
summary:
ResultSummary {
statement: [Object],
statementType: 'rw',
counters: [StatementStatistics],
updateStatistics: [StatementStatistics],
plan: false,
profile: false,
notifications: [],
server: [ServerInfo],
resultConsumedAfter: [Integer],
resultAvailableAfter: [Integer] } } ...
问题是你的getIds
函数。 你已经在另一个承诺中包装了一个已经承诺的返回函数。 第二个问题是你的 getIds 函数。 您正在 forEach 中进行异步操作,这将不起作用。 Promise.all
应该是这样的
async function getIds() {
const result = await session.run("MATCH (m :Movie) RETURN m.imdbId LIMIT 5");
const ids = [];
result.records.forEach(record => ids.push(record._fields[0]));
return ids;
}
async function imdbIds() {
const ids = await getIds();
console.log(ids);
const response = await Promise.all(ids.map(async id => {
// webscraper that returns poster url of each imdbid
const url = toString(scraper(id));
console.log(url);
// query to set poster of each id in neo4j db
const result = await session.run("MATCH (m :Movie {imdbId : $id}) SET m.poster = $url RETURN m.poster", {id, url});
result.records.forEach(record => console.log(record._fields[0]));
return result;
}));
console.log(response);
}
想通了,感谢先生的帮助。 阿什莫迪
async function setPosters() {
const result = await session.run("MATCH (m :Movie) RETURN m.imdbId LIMIT 15");
result.records.forEach(async record => {
const imdbId = record._fields[0];
const url = await scraper(imdbId);
await session.run("MATCH (m :Movie {imdbId : $imdbId}) SET m.poster = $url RETURN m.poster", { imdbId, url });
});
}
setPosters();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.