我想创建一个NodeJS程序,从数据库中提供JSON-aray。我正在使用express,sqlite和sqlite3软件包。当我的代码在终端中运行它时,我得到这个输出:
$ node index.js
[
{ Field1: 'Stockholm', Field2: '123' },
{ Field1: 'Gothenburg', Field2: '123' },
{ Field1: 'London', Field2: '123' }
]
它显示正确的数据。
这是我的代码:
const express = require('express')
const sqlite = require('sqlite')
const sqlite3 = require('sqlite3')
const app = express()
let database
sqlite
.open({ driver: sqlite3.Database, filename: 'test.sqlite' })
.then((database) => {
database.all('SELECT * FROM cities').then(rows => {
console.log(rows)
})
})
app.get('/', (request, response) => {
database.all('SELECT * FROM cities').then(cities => {
response.send(cities)
})
})
app.listen(3000)
当我运行上面的代码时,我收到一条错误消息,说:http://localhost:3000TypeError: Cannot read property 'all' of undefined
我想显示与 终端/控制台中显示的相同数据http://localhost:3000
我的代码出了什么问题?
鸿蒙传说
心有法竹
相关分类