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.
CodePudding user response:
You need to do await create.json()
otherwise createjson
will be a promise. meaning createjson.id
will simply return undefined
because the promise doesn't have an attribute id.
CodePudding user response:
The Response.json() method returns a promise. So you need to await
it
const createjson = await create.json();