gpt4 book ai didi

mysql - 使用 mysql 查询数据更新 "Number"小部件 - Dashing

转载 作者:行者123 更新时间:2023-11-29 18:59:02 26 4
gpt4 key购买 nike

我对 ruby​​ 非常陌生,我正在尝试设置一个测试仪表板。我看到mysql给出了sql查询的总数。但是它没有填充在小部件上。有人可以帮忙吗?

# mysql.rb (jobs file)    
require "mysql2"

SCHEDULER.every '5s' do
client = Mysql2::Client.new(:host => "192.168.1.3",:username => "testuser",:database => 'testdb')

results=client.query("SELECT count(*) as total from testdb.share")
puts results

results.each do |row|
puts row['total']
send_event('totals', { value: total })
end
end

仪表板文件:

# sample.erb
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="totals" data-view="Number" data-title="Number of Shares" data-moreinfo="Shares" data-prefix="$"></div>
</li>
....

Dashing控制台如下所示

#<Mysql2::Result:0x007f98b4b85d18> 
3 --->>>> This is the count of my mysql query

调度程序捕获异常:

> undefined local variable or method `total' for main:Object
> /home/user/.rvm/sweet_dashboard_project/jobs/mysql.rb:20:in `block (2
> levels) in <top (required)>'
> /home/user/.rvm/sweet_dashboard_project/jobs/mysql.rb:11:in `each'
> /home/user/.rvm/sweet_dashboard_project/jobs/mysql.rb:11:in `block in
> <top (required)>'
> /home/user/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
> `call'
> /home/user/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
> `trigger_block'
> /home/user/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in
> `block in trigger'
> /home/user/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
> `call'
> /home/user/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
> `block in trigger_job'

最佳答案

问题出在这里:

results.each do |row|
puts row['total']
send_event('totals', { value: total })
end

检查行send_event('totals', { value:total })。您将 value 设置为 total,但尚未在任何地方设置该变量或方法(即 total),因此 undefined局部变量或方法错误。

它认为您想改为分配row['total']:

results.each do |row|
puts row['total']
send_event('totals', { value: row['total'] })
end

更新

要在小部件中显示值,您必须使用 current 而不是 value 来获取正确的绑定(bind)(在 send_event() 中) ,所以上面的代码就变成了:

results.each do |row|
puts row['total']
send_event('totals', { current: row['total'] })
end

另一种方法是在源文件 widgets/number/number.html 中将 current 更改为 value

所以这一行:

<h2 class="value" data-bind="current | shortenedNumber | prepend prefix | append suffix"></h2>

会变成:

<h2 class="value" data-bind="value | shortenedNumber | prepend prefix | append suffix"></h2>

虽然我不推荐这样做,但最好保留默认值(即当前)。

关于mysql - 使用 mysql 查询数据更新 "Number"小部件 - Dashing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44004235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com