TypeError:数据不可迭代

时间:2018-07-01 13:36:53

标签: html node.js database sqlite

我正在尝试Node.js和sqlite数据库的工作,并且碰到了这一点,我目前陷入困境。

我的控制台错误是:

TypeError: data is not iterable
    at db.all (C:\Users\Taylor\Desktop\book\index1.js:30:22)
    at Statement.errBack (C:\Users\Taylor\node_modules\sqlite3\lib\sqlite3.js:16:21)
--> in Database#all('SELECT id, title FROM Jobs;', [Function])
at app.get (C:\Users\Taylor\Desktop\book\index1.js:26:6)
at Layer.handle [as handle_request] (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Taylor\Desktop\book\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\Taylor\Desktop\book\node_modules\body-parser\lib\types\urlencoded.js:91:7)

这是我的js脚本:

app.get('/', async(req, res) => {
  const sql = 'SELECT id, title FROM Jobs;'
  console.log(sql)
  db.all(sql, (err, data) => {
    if(err) console.error(err.message)
    console.log(data)
    let list = '<ol>'
    for(const job of data) {
      console.log(job.title)
      list += `<li><a href="/details/${job.id}">${job.title}</a></li>`
    }
    list += '</ol>'
    console.log(list)
    res.render('index', {locals: {Jobs: list}})
  })
})

0 个答案:

没有答案