1

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
Abhijith Madhav
  • 2,748
  • 5
  • 33
  • 44
  • 1
    The [typeof](http://es5.github.com/#x11.4.3) operator returns a string, so `typeof == null` will never be true. You should test against one of the possible outcomes ("string", "object", etc.). Whether you use `==` or `===` for that makes no difference since both will be type String. – RobG Mar 16 '12 at 04:34
  • What is the actual value that is being assgned to *out* when there are tuples and when there are not? You might also want to read about the [let operator](https://developer.mozilla.org/en/JavaScript/Reference/Statements/let), which is JavaScript but not ECMAScript. Which is gnome-shell based on? – RobG Mar 16 '12 at 05:34
  • @RobG, When there are tuples, 'out' is a string containing those tuples. When there aren't, any reference to 'out' crashes the gnome-shell. I think gnome-shell is based on the JavaScript(Will confirm) as I copied the mentioned statement from a perfectly working gnome-shell extension. – Abhijith Madhav Mar 16 '12 at 06:05

1 Answers1

0

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.

Abhijith Madhav
  • 2,748
  • 5
  • 33
  • 44