简体   繁体   English

我正在尝试使用 JavaScript 将海报 URL 添加到我的 neo4j 电影数据库,但我一直收到此未定义对象错误

[英]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.

相关问题 我收到错误消息:```TypeError: Object(...) is not a function``` in my react app- 第一次尝试使用钩子 - 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 getting an "undefined" message in my console when trying to add a product to my cart 尝试遍历我的 json 时,我变得未定义 - I'm getting undefined when trying to iterate through my json 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props 我可以使用 Javascript 查询 neo4j 数据库吗? - Can I query the neo4j database with Javascript? 尝试使用d3.time.format格式化日期时,我收到JavaScript错误“d.getMonth不是函数” - I'm getting JavaScript error “d.getMonth is not a function” when trying to format a date using d3.time.format 我在使用 React 制作的项目中遇到未定义的错误 - I'm getting an undefined error in my project made with React 为什么我使用布尔对象变得不确定? - Why i'm getting undefined using Boolean object? 我试图从 Node.js 中的 MySQL 数据库中获取前 3 个结果,但出现此错误 - I'm trying to get first 3 results from MySQL database in Node.js and I'm getting this error 我正在尝试解析我打开的标签的所有网址 - I'm trying to parse all the urls of the tabs I have open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM