1

I'm going through a tutorial below

https://developer.apple.com/library/iad/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/UsingtheJavascriptDatabase.html

and am having trouble converting js to cljs in the dataHandler function

function dataHandler(transaction, results)
{
// Handle the results
var string = "Green shirt list contains the following people:\n\n";
for (var i=0; i<results.rows.length; i++) {
    // Each row is a standard JavaScript array indexed by
    // column names.
    var row = results.rows.item(i);
    string = string + row['name'] + " (ID "+row['id']+")\n";
}
alert(string);
}

Here is my cljs code

 (defn success-handler [tx, results]
   (println "success handler")
   (println results)

   ;;the below doesn't work, I get an error here  
   ;;:Uncaught Error: [object SQLResultSet] is not ISeqable

  (doseq [result results]
  (prn result)))

So my question is how would I convert the js dataHandler to my cljs success-handler?

2 Answers 2

2

Try turning the rows of the result into a sequence, rather than the whole result.

(defn success-handler [tx, results]
   (println "success handler")
   (println results)

  (doseq [result (.-rows results)]
    (prn result)))

And if rows does not implement Iterable then do it the old fashioned way

(defn success-handler [tx, results]
   (println "success handler")
   (println results)

  (let [rows (.-rows results)]
   (doall
    (for [i (range (.-length rows))]
      (let [row (nth rows i)]
        (prn result))))))
Sign up to request clarification or add additional context in comments.

5 Comments

I believe this is getting me closer to the solution, but when I try both options I get the error "Uncaught TypeError: results.rows is not a function" My first thought was to create an anon fn outside of (.rows results) with no luck.
rows is a property not a function, so you need .- instead of just .
sorry, yes i forgot to put it in ClojureScript format. I fixed it in the answer
Is (item i) supposed to have .- in front of item?
sorry, that was a cut-and-paste fail, it should have been just i (I can't actually test this code, so i guess it's more of a guideline)
0

Here is the solution:

(defn success-handler [tx, results]
  (let [rows (.-rows results)]
    (doseq [i (range (.-length rows))]
      (prn (.item rows i)))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.