创建一系列毁灭的一种嵌套的对象

0

的问题

我需要建立一个系列的 id:'s范 entity: 变量内的嵌套 stakes 变量的对象 proposal. 当我使用这个代码解构的对象:

const {
   stakes: {
      entity: { id: SupportersId },
    },
 } = proposal
  console.log(SupportersId)

我得到的错误: TypeError: Cannot read property 'id' of undefined

这是一个`风险'可变的例子套内提议的对象,我应用的代码,是输出运行:

const {stakes} = proposal
console.log(stakes)

输出:

0: 
 amount: BigNumber 
    c: [100000] 
    e: 19 
    s: 1 
    __proto__: Object 
 createdAt: 1616006864000 
 entity: {id: "0x60893...734",
        __typename: "Supporter"} 
 id: "appAddress:0x8blahblah...entity:0x608same" 
 proposal: null 
 type: "Add" 

时运行:

const {stakes: {entity}, }= proposal 
console.log(entity)

我得到:

4 Undefined
10 Undefined
10 Undefined
...

这是一个迭代 console.log(JSON.stringify(proposal))

{"id":"28","number":"28",
    "creator":"0x6089368594bf2d3734",
    "status":"Disputed","createdAt":1621957554000,
    "executedAt":0,"metadata":"test","link":"https://",
"stakes":[{"amount":"658768115942029002",
    "createdAt":1626278001000,
    "entity":{"id":"0x80db3372988d852a"},
    "id":"appAddress:0x0ccdedf21399-proposalId:0x1c- 
          entity:0x80db3aeb089372988d852a",
    "type":"Withdraw","proposal":null}],
"stakesHistory":[{"conviction":"0","createdAt":1622175449000,
    "entity":{"id":"0x80db3a571952988d852a"},
    "id":"appAddress:0x0cca32354b5da7dedf21399-proposalId:0x1c- 
          entity:0x80db3a571372988d852a-time:8662647",
"time":"8662647","tokensStaked":"86275362318840579720",
"totalTokensStaked":"86275362318840579720","type":"Add","proposal":null},
{"conviction":"2.156363897696420943854e+21",
"createdAt":1622175824000,"entity":{"id":"0x80db3a5719988d852a"},
"id":"appAddress:0x0ccda7dedf21399-proposalId:0x1c-entity:0x80db3a5b089372988d852a-time:8662672","time":"8662672","tokensStaked":"86202898550724637692",
"totalTokensStaked":"86202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"3.8533152129053758738146e+22",
"createdAt":1622182185000,
"entity":{"id":"0x80db3a57
2988d852a"},
"id":"appAddress:0x0cc6f0962b415
f21399-proposalId:0x1c-entity:0x80db3a5
72988d852a-time:8663096","time":"8663096","tokensStaked":"84202898550724637692",
"totalTokensStaked":"84202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.114296303522575335279954e+24",
"createdAt":1622406093000,"entity":{"id":"0x80db3a57195616eb3ccbb2b6eb089372988d852a"},"id":"appAddress:0x0cc6
1399-proposalId:0x1c-entity:0x80d
089372988d852a-time:8678021","time":"8678021","tokensStaked":"64202898550724637692",
"totalTokensStaked":"64202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.1372042
9e+24","createdAt":1622414360000,"entity":{"id":"0x80db3a
852a"},"id":"appAddress:0x0cc6
dedf21399-proposalId:0x1c-entity:0x80db3a
852a-time:8678572","time":"8678572","tokensStaked":"4202
37692","totalTokensStaked":"4202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.056
011e+24","createdAt":1622481968000,"entity":{"id":"0x8
2988d852a"},"id":"appAddress:0x0cc6
21399-proposalId:0x1c-entity:0x80
372988d852a-time:8683078","time":"8683078","tokensStaked":"2202898550724637692",
"totalTokensStaked":"2202898550724637692","type":"Withdraw","proposal":null},
{"conviction":"2.91692914179098548409009e+23","createdAt":1623713056000,"entity":{"id":"0x80db3a5
d852a"},"id":"appAddress:0x0cc
df21399-proposalId:0x1c-entity:0x80db3a57
8d852a-time:8765118","time":"8765118","tokensStaked":"2180833333333333347",
"totalTokensStaked":"2180833333333333347","type":"Withdraw","proposal":null},
{"conviction":"1.14417968316260986028367e+23","createdAt":1626276470000,"entity":{"id":"0x80d
8d852a"},"id":"appAddress:0x0cc6f0
f21399-proposalId:0x1c-entity:0x80db3
8d852a-time:8935883","time":"8935883","tokensStaked":"680833333333333347",
"totalTokensStaked":"680833333333333347","type":"Withdraw","proposal":null},
{"conviction":"1.14253001105365595015519e+23","createdAt":1626278001000,"entity":{"id":"0x80db3a57195616eb3ccbb2b6eb089372988d852a"},"id":"appAddress:0x0c
1399-proposalId:0x1c-entity:0x80d
d852a-time:8935985","time":"8935985","tokensStaked":"658768115942029002",
"totalTokensStaked":"658768115942029002","type":"Withdraw","proposal":null},
{"conviction":"3.2774663072910697629524e+22","createdAt":1636282624000,
"entity":{"id":"0x74b232255dc
5951d"},"id":"appAddress:0x0c
1399-proposalId:0x1c-entity:0x74b2
375951d-time:9600096","time":"9600096","tokensStaked":"1000000000000000000",
"totalTokensStaked":"1658768115942029002","type":"Add","proposal":null},
{"conviction":"3.3231555602660371350587e+22","createdAt":1636289513000,"entity":{"id":"0x74b232255
51d"},"id":"appAddress:0x
21399-proposalId:0x1c-entity:0x74b2
75951d-time:9600555","time":"9600555","tokensStaked":"0",
"totalTokensStaked":"658768115942029002","type":"Withdraw","proposal":null}],"beneficiary":"0x000000
000","requestedAmount":"0","totalTokensStaked":"658768115942029002",
"stable":false,"setting":null,"startDate":null,"totalPower":null,
"snapshotBlock":"0","yeas":null,"nays":null,"quietEndingExtensionDuration":null,
"quietEndingSnapshotSupport":null,"script":null,"isAccepted":null,
"casts":[],"actionId":"30","challengeId":"17","challenger":"0xc
020c","challengeEndDate":1622295426000,"disputeId":"10","settledAt":0,
"settlementOffer":"100000000000000000","disputedAt":1622036751000,
"pausedAt":1622036226000,"pauseDuration":0,"collateralRequirement":{"id":"appAddress:0x0cc6f
1399-proposalId:0x1c","proposalId":"appAddress:0x0cc6f
edf21399-proposalId:0x1c","tokenId":"0x3050
a4f9","tokenDecimals":18,"tokenSymbol":"HNYT","actionAmount":"100000000000000000",
"challengeAmount":"100000000000000000","challengeDuration":"259200"},
"submitterArbitratorFee":null,"challengerArbitratorFee":null,"name":"test",
"convictionTrend":"-2.248532625135e-8","currentConviction":"3.28405338806e+22",
"futureConviction":"3.2774+22","futureStakedConviction":"0.00010213206901472402",
"hasEnded":false,"maxConviction":"3.2090344832144279e+26",
"stakedConviction":"0.0001023377406715919","statusData":{"disputed":true},"userConviction":"0","userStakedConviction":"0","requestedAmountConverted":"0"}

希望有人可以帮我这个。 我觉得我需要添加一个指数的变量的办法,你会当映通过一系列创建一个元素,但是,我还不肯定在什么这看起来就像无论是,可悲的。 最后,我要通过这个清单作为一个孩子到另一个功能,将图通过它在另一种形式在我的应用程序以拉图像和资料数据从id创建一个"人卡"如果有帮助。 任何帮助或建议,将不胜感激,谢谢!

1

最好的答案

1

这里有一个例子,destructures正确。 在这个例子中,我解构仅仅是第一个对象你的 proposal.stakes array(即: proposal.stakes[0]).

const proposal = {
    stakes: [{
        amount: 10n,
        c: [100000],
        e: 19,
        s: 1,
        createdAt: 1616006864000,
        entity: {
            id: "0x60893...734",
            __typename: "Supporter"
        },
        id: "appAddress:0x8blahblah...entity:0x608same",
        proposal: null,
        type: "Add",
    }]
};

const {
    stakes: [{
        entity: { id: SupportersId },
    }],
} = proposal;
console.log(SupportersId)

const {stakes: [{entity}], }= proposal;
console.log(entity);

2021-11-24 05:17:45

提议的对象是东西,我正在通过后台。 我想要创造的SupportersId变量地图的通过以后在应用程序在另一个组成部分。
Striker

@前锋,我更新我的职务,以对个事实,即你的 stakes 酒店是一系列的对象。
Wyck

威克,你的男人! 谢谢你们向我展示什么我失踪了。 我很欣赏你的努力和知识! 欢呼
Striker

@前锋,我好好的反馈意见。 你可以点击勾接受这个答案,这是典型的方式,我们说"你是男人"在一起的溢出。 ;)
Wyck

许多限制功能于一个新的用户喜欢的 @-用户和击,检查。 但不得不说的,所以我做了:)
Striker

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................