[英]Converting JavaScript websql to Clojurescript
I'm going through a tutorial below 我正在看下面的教程
https://developer.apple.com/library/iad/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/UsingtheJavascriptDatabase.html https://developer.apple.com/library/iad/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/使用JavascriptDatabase /使用JavascriptDatabase.html
and am having trouble converting js to cljs in the dataHandler function 并在dataHandler函数中将js转换为cljs时遇到麻烦
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 这是我的cljs代码
(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? 所以我的问题是如何将js dataHandler转换为cljs成功处理程序?
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 如果行未实现Iterable,则以老式方式进行
(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))))))
Here is the solution: 解决方法如下:
(defn success-handler [tx, results]
(let [rows (.-rows results)]
(doseq [i (range (.-length rows))]
(prn (.item rows i)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.