Hi! I moved forward with the course, but I am still getting the “Unexpected token” error:
Here is a screenshot of my dApp window:
Now following the course and Filip’s instructions, I added the addDog()
function in my code.
When I try to insert a dog name and the dog age in the text boxes and click the Add button (the Add Dog feature of the dApp), I am getting another error:
I checked line 94 of my JS code: eos.transact({
and verified the whole addDog()
function by comparing it to Filip’s code in the video, but I didn’t spot any error.
I reached the end of the Add Dog Table Update section. I would like to do the Assignment - Remove Dog exercise, but I cannot test the result of my code in my dApp, because of these two errors, that don’t allow me to see and refresh the list of dogs and to use the Add Dog feature, so I cannot test my code.
Here is my main.js code now:
ScatterJS.plugins( new ScatterEOS() );
const network = ScatterJS.Network.fromJson({
blockchain:'eos',
chainId:'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
host:'127.0.0.1',
port:8888,
protocol:'http'
});
const contractConfig = {
code: "dogctrissuer",
scope: "dogctrissuer",
dogTableName: "dogs",
balancesTableName: "balances",
symbol: "DOGCOIN"
}
const scatter = ScatterJS.scatter;
var eos;
var rpc; //fetch data from blockchain
var account;
//initialize function to scatter and webpage functions
ScatterJS.connect('DogDapp', {network}).then(connected => {
//check if scatter is running
if(!connected) return alert("No Scatter Detected");
console.log("Scatter Connected");
window.ScatterJS = null;
});//end ScatterJS.connect function
function logIn(){
//scatter login popup window
scatter.login({accounts: [network]}).then(function(){
account = scatter.account('eos');
//get data from blockchain into a Json
rpc = new eosjs_jsonrpc.JsonRpc(network.fullhost());
//initialize eos object to create transactions
eos = scatter.eos(network, eosjs_api.Api, {rpc});
//get table data functions
getAccount();
getBalance();
getDogs();
});
}//end logIn
function logOut(){
scatter.logout({accounts: null}).then();
}//enf logOut
//get name account
function getAccount(){
$("#Account").empty();
var AccId = document.getElementById("Account");
var AccName = account.name;
AccId.innerHTML = AccName;
}//end getAccount
//function to get data from blockchain tables
function getDogs(){
rpc.get_table_rows({
json: true,
code: contractConfig.code,
scope: contractConfig.scope,
table: contractConfig.dogTableName,
index_position: 2,
key_type: "name",
lower_bound: account.name,
upper_bound: account.name
}).then(function(res){
console.log(res);
populateDogList(res.rows);
})
}//end getDogs()
function populateDogList(dogs){
$("#doglist").empty();
var ul = document.getElementById("doglist");
for (var i = 0; i < dogs.length; i++) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(dogs[i].id + ": " + dogs[i].dog_name + ", " + dogs[i].age));
ul.appendChild("li");
}
}
function addDog(){
var dogName = $("#dog_name").val();
var dogAge = $("#dog_age").val();
eos.transact({
actions: [{
account: contractConfig.code,
name: 'insert',
authorization: [{
actor: account.name,
permission: account.authority
}],
data: {
owner: account.name,
dog_name: dogName,
age: dogAge
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30
}).then(function(res){
// SUCCESSFUL ADD
getDogs();
}).catch(function(err){
alert(err);
})
}
//wait until html page is loaded and apply some logic to objects
$(document).ready(function() {
//Add dog button function
$("#add_button").click(addDog);
//Delete dog button function
$("#del_button").click(deleteDog);
//Delete ALL button function
$("#del_all_button").click(deleteAll);
//Deposit coins into contract
$("#deposit_button").click(deposit_coin);
//logIn and logOut buttons
$("#login_button").click(logIn);
$("#logout_button").click(logOut);
});
Your help would be appreciated @thecil @ivga80 @filip
Please kindly note that I submitted the “Unexpected token < in JSON at position 0” issue to the EOS.IO Stack Exchange forum. Let’s see if I get an answer soon.