Help with syntax error

I’m getting a syntax error:

Unexpected identifier ‘_vmin’. Expected ‘;’ after variable declaration.

but I can’t figure out for the life of me why…

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: tv;
	let _auth = "Basic XXXXXXXXXXX";
	
	let data = getDomains(_auth);
	// console.log(json)
	// let table = new UITable()
	let table = new UITable()
	table.showSeparators = true
	populateTable(data)
	table.present()

function getDomains(auth)
{
	let _url = "https://api.domain.com/somecall/&json=1";
	let _vmin = new Request(_url);
	_vmin.headers = {"Authorization" : auth }
	let json = await _vmin.loadJSON()
	return json.data
}

function populateTable(data)
{
	table.removeAllRows()
	for(domain of data)
	{
		// console.log(domain)
		let row = new UITableRow()
		row.dismissOnSelect = false
		row.onSelect = (idx) => {
	      let selectedDomain = data[idx]
	      // getDomainInfo(selectedDomain,_auth)
	    }
	    let titleCell = row.addText(domain.name)
	    table.addRow(row)
	}
}

function getDomainInfo(domain,auth)
{
	let _url = "https://api.domain.com/someothercall/&domain=" + domain + "&include-owner&multiline&json=1";
	let req = new Request(_url);
	req.headers = {"Authorization" : auth }
	let json = await req.loadJSON()
	let _data = json.data[0]
	let name = _data.values.unix_username[0]
	let pw = _data.values.password[0]
	// let alert = new Alert()
	// alert.addAction(name)
	// alert.addAction(pw)
	// alert.present()
	console.log(pw)
}

is anything jumping out to anyone?

I think it has to do with your awaits. Are you Promising anything anywhere for those async calls?

Dunno… first time I’ve ever mucked with ‘await’ if I remove the function getDomains() and just have the code inline at the top of the page, it doesn’t throw an error. but then it does for the getDomainInfo() function…

That’s because there’s another await in that function too.

So can you not use ‘await’ in a function?

You can, but I think there’s more to it than just tossing in awaits. I’m not 100% sure on how scriptable handles this either. Perhaps someone with more knowledge on the subject can chime in here. I’m no expert on awaits, but I have read this article a few times when I’ve needed to deal with them. Maybe it’ll help you too?

Thanks for the link… I’ll be googling around for a bit, I’m sure.

I’m halfway there. I marked where it errors

SyntaxError: Unexpected identifier ‘getDomainInfo’. Expected ‘;’ after variable declaration.

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: tv;
    let _auth = "Basic xxxxxxxxxxxxxxxxxxxx";
    
    let data = await getDomains(_auth);
    
    let table = new UITable()
    table.showSeparators = true
    populateTable(data.data)
    table.present()

function getDomains(auth)
{
    return new Promise((resolve,reject) => {
        // console.log("auth: " + auth)
        let _url = "https://api.domain.com/acall/&name-only&json=1";
        // console.log(_url);
        let _vmin = new Request(_url);
        _vmin.headers = {"Authorization" : auth }
        let json = _vmin.loadJSON()
        // console.log(json);
        resolve(json);
    });
    
}

function populateTable(data)
{
    table.removeAllRows()
    for(domain of data)
    {
        // console.log(domain)
        let row = new UITableRow()
        row.dismissOnSelect = false
        row.onSelect = (idx) => {
          let selectedDomain = data[idx]
          let res = await getDomainInfo(selectedDomain,_auth); <--------- THROWS ERROR HERE
          console.log(res.data)
        }
        let titleCell = row.addText(domain.name)
        table.addRow(row)
    }
}

function getDomainInfo(domain,auth)
{
    return new Promise((resolve,reject) => {
        let _url = "https://api.domain.com/asecondcall/&domain=" + domain + "&include-owner&multiline&json=1";
        let req = new Request(_url);
        req.headers = {"Authorization" : auth }
        let json = req.loadJSON()
        resolve(json);
    });
}

anyone know if this logic is even possible. The hope is to execute another call when a cell is selected

You are using await in a function that isn’t async. That’s not allowed. Unfortunately, JavaScript has poor error handling in these cases but when you get to know them, you can pretty quickly spot them.

Declaring a function as async basically means that JS will treat the dunction as returning a promise and that the function can be called with await. It also allows you to use await in the function body.

So getDomains should be declared as:

async function getDomains(auth) {
  // ...
}

And getDomainInfo should be declared as:

async function getDomainInfo(domain, auth) {
  // ...
}
1 Like

Boom. Thanks @simonbs!

I see that we posted a reply at the same time and that you’ve taken a different approach. For completeness sake, I’ll post your script with my suggested fixes. I haven’t tested if the script works but the changes should address the issue you’re raising here.

let _auth = "Basic XXXXXXXXXXX";

let data = await getDomains(_auth);
// console.log(json)
// let table = new UITable()
let table = new UITable()
table.showSeparators = true
await populateTable(data)
table.present()

async function getDomains(auth)
{
	let _url = "https://api.domain.com/somecall/&json=1";
	let _vmin = new Request(_url);
	_vmin.headers = {"Authorization" : auth }
	let json = await _vmin.loadJSON()
	return json.data
}

async function populateTable(data)
{
	table.removeAllRows()
	for(domain of data)
	{
		// console.log(domain)
		let row = new UITableRow()
		row.dismissOnSelect = false
		row.onSelect = (idx) => {
	      let selectedDomain = data[idx]
	      // getDomainInfo(selectedDomain,_auth)
	    }
	    let titleCell = row.addText(domain.name)
	    table.addRow(row)
	}
}

async function getDomainInfo(domain,auth)
{
	let _url = "https://api.domain.com/someothercall/&domain=" + domain + "&include-owner&multiline&json=1";
	let req = new Request(_url);
	req.headers = {"Authorization" : auth }
	let json = await req.loadJSON()
	let _data = json.data[0]
	let name = _data.values.unix_username[0]
	let pw = _data.values.password[0]
	// let alert = new Alert()
	// alert.addAction(name)
	// alert.addAction(pw)
	// alert.present()
	console.log(pw)
}
1 Like

Thanks. Your suggestion is much simpler to follow

I’ve changed the code adding the ‘async’ but it still says ‘getDomainInfo’ is an unexpected identifier.

async function populateTable(data)
{
    table.removeAllRows()
    for(domain of data)
    {
        // console.log(domain)
        let row = new UITableRow()
        row.dismissOnSelect = false
        row.onSelect = (idx) => {
          let selectedDomain = data[idx].name;
          console.log(selectedDomain);
          let res =  await getDomainInfo(selectedDomain,_auth);
          console.log(res);
        }
        let titleCell = row.addText(domain.name)
        table.addRow(row)
    }
}

if I remove the “await” before getDomainInfo the error disappears, and the call goes out. I can see that in Charles, but of course it’s too late…

I would think that await isn’t actually allowed in onSelect. It’s an anonymous function and it isn’t async. This seems to be one of those (rare-ish) situations where you’d use .then over await. Something like the following.

function populateTable(data)
{
    table.removeAllRows()
    for(domain of data)
    {
        // console.log(domain)
        let row = new UITableRow()
        row.dismissOnSelect = false
        row.onSelect = (idx) => {
          let selectedDomain = data[idx].name;
          console.log(selectedDomain);
          getDomainInfo(selectedDomain,_auth).then(res => {
            console.log(res);
         })
        }
        let titleCell = row.addText(domain.name)
        table.addRow(row)
    }
}
1 Like

That worked. Thank you. Thank you.