gpt4 book ai didi

javascript - 我正在尝试使用 JavaScript 将海报 URL 添加到我的 neo4j 电影数据库,但我总是收到此 undefined object 错误

转载 作者:行者123 更新时间:2023-12-02 22:07:47 25 4
gpt4 key购买 nike

我对 JavaScript 完全陌生,很难理解异步调用。我是否需要嵌套另一个 Promise 对象来设置海报 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 函数上。你已经将一个已经 promise 返回的函数包装在另一个 promise 中。第二个问题是你的 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);
}

关于javascript - 我正在尝试使用 JavaScript 将海报 URL 添加到我的 neo4j 电影数据库,但我总是收到此 undefined object 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59664551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com