กำลังสร้างเป็นอาเรย์ destructing เป็นรอบการวัตถุ

0

คำถาม

ฉันต้องการจะสร้างตารางคู่ลำดับของ id:'s ภายใน entity: ตัวแปรอบการภายใน stakes ตัวแปรในวัตถุ proposal. ตอนที่ฉันใช้รหัส destructure งวัตถุ:

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"}

หวังว่าจะมีบางคนสามารถช่วยปลดนี่ให้ฉันหน่อย ฉันรู้สึกเหมือนกับว่าฉันต้องเพิ่มเป็นจำนวนดัชนีที่ตัวแปรในวิธีที่คุณจะตองวางแผนจะก้าวผ่านอาเรย์ต้องสร้างธาตุอย่างไรก็ตามฉันยังไม่แน่ใจว่านั่นจะดูเหมือนเหมือนกันน่าเศร้า. ในที่สุดฉันจะส่งรายการนี้เหมือนเด็กคนอื่นฟังก์ชันที่จะทำแผนที่ผ่านมันในอีกรูปแบบในของโปรแกรมเกี่ยต้องดึงภาพและโพรไฟล์ข้อมูลจากบัตรเพื่อสร้างเป็น"ส่วนตัวการ์ด"ก็ได้นะถ้ามันช่วย ช่วยหรือคำแนะนำจากค greatly ซาบซึ้งขอบคุณ!

1

คำตอบที่ดีที่สุด

1

นี่เป็นตัวอย่างนั้น destructures อย่างถูกต้อง ในวอย่างเช่นฉัน destructure แค่คนแรกวัตถุอยู่ของคุณ proposal.stakes อาเรย์(i.e.: 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

Wyck,คุณเป็นพวกผู้ดีชาย! ขอบคุณมากสำหรับแสดงสิ่งที่ฉันหายตัวไป ฉันขอบคุณความพยายามและความรู้! เชีย
Striker

@สไตรเกอร์ผมขอบคุณที่การแจ้งเตือนการเริ่มทำงาน! คุณสามารถคลิกที่ checkmark ต้องยอมรับเรื่องนี้ตอบนั่นคือความช่วยเหลือจากทางที่เราพูดว่า"คุณเป็นผู้ชาย"อยู่ที่งานเลยล้นมาถึง. ;)
Wyck

หลายครั้งฟังก์ชันเพื่อนใหม่เหมือนของผู้ใช้ @-ของผู้ใช้และการคลิกเมาส์ปุ่นเช็คนั่น แต่ต้องบอกว่าดังนั้นฉันทำ:)
Striker

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่