[英]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
I'm completely new to JavaScript and having a hard time trying to understand asynchronous calls.我对 JavaScript 完全陌生,并且很难理解异步调用。 Do I need to nest another promise object to set the poster URLs?
我是否需要嵌套另一个承诺对象来设置海报 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();
Console logs still no luck:控制台日志仍然没有运气:
[ '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] } } ...
The problem is your getIds
function.问题是你的
getIds
函数。 you have wrapped a already promise returning function in another promise.你已经在另一个承诺中包装了一个已经承诺的返回函数。 second problem is your getIds function.
第二个问题是你的 getIds 函数。 you are doing asynchronous operation in forEach which will not work.
您正在 forEach 中进行异步操作,这将不起作用。 Replace it with
Promise.all
it should be like this 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);
}
figured it out, thanks for help mr.想通了,感谢先生的帮助。 ashish modi
阿什莫迪
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.