![](/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.