I’m learning AlgoSDK (JS) with DJango, and got stuck getting accountInformation(). If I try the below without DJango, it all works.
const algosdk = require("algosdk");
const my_address = "PTTKX5....C2MOVPM"
const algodServer = "http://127.0.0.1";
const algodPort = 35493;
const algodToken = "5685be....234c26b1";
const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
(async () => {
let accountInfo = await algodClient.accountInformation(my_address).do();
console.log(accountInfo);
}) ().catch(e => {
console.log(e);
});
But with DJango, it causes “ReferenceError: Cannot access ‘algosdk’ before initialization”
Please help.
wAlgoConnect.js
const algosdk = require("algosdk"); // causes ReferenceError: cannot access 'algosdk' before initialization
const algodServer = "http://127.0.0.1";
const algodPort = 35493;
const algodToken = "5685bef....c0234c26b1";
async function getBalance() {
const addr="PTTKX5V,,,,YC2MOVPM";
try {
const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
let accountInfo = await algodClient.accountInformation(addr).do();
alert(accountInfo.amount);
} catch (err) {
alert("Error: getBalance() " + err);
}
}
DJango Template: main.html
{% load static %}
<html>
<head>
<title>Nurky</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}"/>
<link rel="icon" href="{% static 'favicon-16.png' %}" sizes="16x16" type="image/png"/>
<style>
body {
background-image: url("{% static 'images/bg.jpg' %}");
background-repeat: repeat;
}
</style>
<script src="{% static 'js/myalgo.min.js' %}"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{% static 'js/algosdk.min.js' %}"></script>
<script src="{% static 'js/wAlgoConnect.js' %}"></script>
</head>
<body>
..
<div class="wallet-position">
<div id="wContent">
{% if request.session.wAddrShort %}
{{ request.session.wAlgoBalance }}
<a href="{% url 'walletDisconnect' %}">
<button class="wallet-button" type="button" > {{ request.session.wAddrShort }} </button></a>
{% else %}
<button class="wallet-button" type="button" onclick="getBalance()"> Connect Wallet </button>
{% endif %}
</div>
</div>
..