Me and my friend are trying to make a farm game and you have to make a Bitcoin wallet to start playing. I am using the code below and the request goes perfectly, but it doesn’t show the wallet ID; it only shows it as undefined
.
<button onclick="createWallet()">Create Wallet</button>
<script>
async function createWallet() {
const create = await fetch('https://example.org/?type=create', {
method: "GET",
mode: "no-cors",
headers: {
"Content-Type": "application/json"
}
});
const createjson = create.json();
localStorage.setItem("walletid", createjson.id);
alert(`wallet created: ${createjson.id}`);
// window.location.href = `https://example.org/game?wallet=${createjson.id}`;
}
</script>
I tried changing create.json();
to JSON.parse(create);
and still shows it as undefined
, and I am supposed to get the created wallet ID. Yes, the server is giving out a JSON response, something like this: {"id":"examplethinghere"}
. By the way, I have searched and none of the questions have helped me either.
2
Answers
You need to do
await create.json()
otherwisecreatejson
will be a promise. meaningcreatejson.id
will simply returnundefined
because the promise doesn’t have an attribute id.The Response.json() method returns a promise. So you need to
await
it