简体   繁体   English

Typescript在对象上返回undefined

[英]Typescript returns undefined on object

I have the following code. 我有以下代码。 I pull and object from Express Pouchdb then loop through the _attachments key to pull all the attachments. 我从Express Pouchdb拉出对象,然后遍历_attachments键以拉出所有附件。 I have two attachments: "index" and "toc". 我有两个附件:“index”和“toc”。 Fun part is "index" returns it's "data" property fine but this one keeps failing and I have no idea why. 有趣的部分是“索引”返回它的“数据”属性罚款,但这一个保持失败,我不知道为什么。 This is how I have set the "toc" to my article object. 这就是我将“toc”设置为我的文章对象的方法。

Object.keys(new_article['attachments']).map((at) => {
    this.articleStore.getAttachment(new_article['id'],at).then ((res) => {
        new_article.attachments[at]['data'] = res.toString();
    });
});

if (new_article['attachments'].hasOwnProperty('toc')) {
    console.log(new_article['attachments']['toc']) //Line #27 in screenshot
    console.log(new_article['attachments']['toc'].data) //Line #28 in screenshot
    new_article.toc = new_article['attachments']['toc']['data'];
}

Here's the screenshot of the console: 这是控制台的屏幕截图: 控制台的截图

The same object from PouchDB: 来自PouchDB的相同对象:

{
  "_id": "8c0f586b-020b-4832-bcde-945421c22a2e",
  "description": "Getting the Gist of Markdown's Formatting Syntax",
  "author": "John Gruber",
  "title": "Getting the Gist of Markdown's Formatting Syntax",
  "date": "4/15/2017",
  "_attachments": {
    "index.md": {
      "digest": "md5-PjR3R2K+KdpgLy/ye5wgAA==",
      "content_type": "application/octet-stream",
      "length": 8373,
      "revpos": 5,
      "stub": true
    },
    "toc": {
      "digest": "md5-heDcEdGg7nzo0OKe/30YxQ==",
      "content_type": "application/octet-stream",
      "length": 130,
      "revpos": 6,
      "stub": true
    }
  },
  "_rev": "8-167fdf2e930304927c5e2fc4e371b123"
}

The whole project here => https://github.com/flamusdiu/micro-blog/tree/dev 整个项目在这里=> https://github.com/flamusdiu/micro-blog/tree/dev

Edit 1 编辑1

_attachments is remapped to attachments when I pull the object from pouchdb. 当我从pouchdb中拉出对象时, _attachments会重新映射到attachments Updated the code to show the lines above it. 更新了代码以显示其上方的行。

Edit 2 Returning the value from the async call works fine. 编辑2从异步调用返回值工作正常。 There is nothing wrong with that part of it. 这一部分没有错。 Seems that it's chaining that to my second call is what fixed it. 看起来它链接到我的第二个电话是修复它的原因。 See answer below. 见下面的答案。

The DB query is an asynchronous call, the assignments in the loop are not executed before the if at the end. DB查询是异步调用,循环中的赋值不会在if之前执行。 Try this: 尝试这个:

Promise.all(Object.keys(new_article['attachments']).map((at) => {
    return this.articleStore.getAttachment(new_article['id'],at).then ((res) => {
        new_article.attachments[at]['data'] = res.toString();
    });
})).then(() => {
    if (new_article['attachments'].hasOwnProperty('toc')) {
        console.log(new_article['attachments']['toc']) //Line #27 in screenshot
        console.log(new_article['attachments']['toc'].data) //Line #28 in screenshot
        new_article.toc = new_article['attachments']['toc']['data'];
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM