0

I need to create an array of the id:'s within the entity: variable nested within the stakes variable in the object proposal. When I use this code to destructure the object:

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

I get the error: TypeError: Cannot read property 'id' of undefined

This is a `stakes' variable example nested within the proposal object that I am applying the code to that is the output of running:

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

output:

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" 

When running:

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

I get:

4 Undefined
10 Undefined
10 Undefined
...

This is one iteration of 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"}

Hopefully someone can help me with this. I feel like I need to add an index number to the variable in the way that you would when mapping through an array to create an element however, I am also not certain on what that would look like either, sadly. Ultimately I am going to pass this list as a child to another function that will map through it in another form in my app to pull image and profile data from the id to create a "personal card" if that helps. Any help or advice would be greatly appreciated, thanks!

6
  • 1
    your proposal is a list. Not an object. You can use Array.map() to achieve what you want Commented Nov 24, 2021 at 3:11
  • 1
    Does this question really need the blockchain tag? I think not. Commented Nov 24, 2021 at 3:16
  • Please post the input in a JSON format. Your object structure is unclear. JSON.stringify(proposal) Commented Nov 24, 2021 at 3:54
  • I just edited the entire question to hopefully clear up any confusion created on my part. Running stringify() creates a 112kb file per each proposal object. Truly I just need to know how to get past the TypeError...undefined hurdle. Commented Nov 24, 2021 at 4:16
  • May I recommend reporting your object in JSON format here in the question, it's much more clear than a log message from your browser, which has omitted the details of the stakes array of objects, for example. If you do so, you are welcome to remove irrelevant properties from it, so long as the essence of the problem remains (the nested objects within the stakes array.) Commented Nov 24, 2021 at 5:11

1 Answer 1

1

Here's an example that destructures correctly. In this example, I destructure just the first object in your proposal.stakes array (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);

Sign up to request clarification or add additional context in comments.

5 Comments

The proposal object is something that I am being passed from the backend. I want to create the SupportersId variable to map through later in the application with in another component.
@Striker, I updated my post to align with the fact that your stakes property is an array of objects.
Wyck, youre the man! Thanks so much for showing me what I was missing. I appreciate you're effort and knowledge! Cheers
@Striker, I appreciate the feedback! You can click the checkmark to accept this answer, which is the typical way we say "you're the man" at Stack Overflow. ;)
many limited functions for a new user like @-user and clicking that check. But had to be said, so I did :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.