Search code examples
javascriptgnome-shell

Converting javascript object which contains nothing to a string


I am writing an extension for the gnome-shell. The javascript extension spawns a sql query and captures the output on standard output. In cases where the sql query evaluates to 0 tuples/records my extension is crashing.

Below is the code snippet.

let [success, out, err, error] = GLib.spawn_sync(null, ["sqlite3", "-line", places, sqlQuery], null, 4, null);
let result = out.toString(); // Crashing here for 0 tuples. I was expecting 'result = ""'

I am a javascript newbie. I am not understanding how the object out should be handled in this scenario. The object is not null; nor is it undefined.

typeof out == null // false
typeof out == undefined // false
typeof out == "object" // true

EDIT

typeof out == "null" // false
typeof out == "undefined" // false
typeof out == "object" // true

Solution

  • I found out that I could prevent the crash if I did the following

    let [success, out, err, errno] = GLib.spawn_sync(null, ["sqlite3", "-line", places, sqlQuery], null, 4, null);
    if (out.length > 0) {
            let records = out.toString().split('\n\n');
    }
    

    I don't understand why out.toString() couldn't give me an empty string instead of crashing, yet. Hoping I will as I learn more about javascript and glib.