最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

即使插入db,路由返回空数组似乎正在工作

IT培训 admin 3浏览 0评论

即使插入db,路由返回空数组似乎正在工作

我正在学习如何使用Sqlite3和Node,我遇到了一个奇怪的问题。在我的反应前端的主要App.js的componentWillMount()中,我向路线/all提出了一个axios请求,因此我可以填充联系人列表。

奇怪的是,当我点击其他路线时,qazxsw poi在添加联系人时遇到不同的axios请求,它会到达我的qazxsw poi,

/add

稍微延迟,因为我在发出axios请求之前设置了state,这让我觉得联系人被添加到contacts表中。

但是当我直接访问then()时,我收到一个空数组axios .post('/add', contactData) .then(res => console.log(`Contact ${contactData.name} added successfully`) ) .catch(err => console.log('Error encountered: ', err)); 作为响应。我不确定发生了什么。

这是我的server.js

localhost:5000/all

编辑:

我应该注意,当我导航到/所有我得到这个,

[]

当我尝试发布/添加时,我收到错误

const express = require('express'); const sqlite3 = require('sqlite3'); const path = require('path'); const cors = require('cors'); const dbName = 'my.db'; const tableName = 'Contacts'; const dbPath = path.resolve(__dirname, dbName); const app = express(); const port = process.env.PORT || 5000; app.use(cors()); app.listen(port, () => console.log(`Server running on port ${port}`)); app.get('/all', (req, res) => { let db = new sqlite3.Database(dbPath); let sql = `SELECT number FROM ${tableName}`; db.run( `CREATE TABLE IF NOT EXISTS ${tableName}(name text, number text, address text)` ); db.all(sql, [], (err, rows) => { if (err) { return res.status(500).json(err); } else { return res.json(rows); } }); }); app.post('/add', (req, res) => { let db = new sqlite3.Database(dbPath); db.run( `INSERT INTO ${tableName}(name, number, address) VALUES(${req.name},${ req.number },${req.address})`, [], err => { if (err) return res.status(500).json(err); } ); return res.json({ msg: 'success' }); });

不管我在哪里发送多个回复。

回答如下:

每次你点击时我都不会创建你的数据库并创建你的表。

试试这个:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

你的/all路线也看起来有点偏。试试这个:

// get this out of the `/all` route, no need to initialize the db object over and over again
let db = new sqlite3.Database(dbPath);

// also leave this outside the `/all` route:
// no need to create the table over and over again.
db.run(
  `CREATE TABLE IF NOT EXISTS ${tableName}(name text, number text, address text)`
);


app.get('/all', (req, res) => {
  let sql = `SELECT number FROM ${tableName}`;

  // according to the sqlite3 api, the second parameter is optional, so just leave it out:
  db.all(sql, (err, rows) => {
    if (err) return res.status(500).json(err); // if you use return,  you don't need 'else' because the code will never reach it.

    res.json(rows)
  });
});

即使插入db,路由返回空数组似乎正在工作

我正在学习如何使用Sqlite3和Node,我遇到了一个奇怪的问题。在我的反应前端的主要App.js的componentWillMount()中,我向路线/all提出了一个axios请求,因此我可以填充联系人列表。

奇怪的是,当我点击其他路线时,qazxsw poi在添加联系人时遇到不同的axios请求,它会到达我的qazxsw poi,

/add

稍微延迟,因为我在发出axios请求之前设置了state,这让我觉得联系人被添加到contacts表中。

但是当我直接访问then()时,我收到一个空数组axios .post('/add', contactData) .then(res => console.log(`Contact ${contactData.name} added successfully`) ) .catch(err => console.log('Error encountered: ', err)); 作为响应。我不确定发生了什么。

这是我的server.js

localhost:5000/all

编辑:

我应该注意,当我导航到/所有我得到这个,

[]

当我尝试发布/添加时,我收到错误

const express = require('express'); const sqlite3 = require('sqlite3'); const path = require('path'); const cors = require('cors'); const dbName = 'my.db'; const tableName = 'Contacts'; const dbPath = path.resolve(__dirname, dbName); const app = express(); const port = process.env.PORT || 5000; app.use(cors()); app.listen(port, () => console.log(`Server running on port ${port}`)); app.get('/all', (req, res) => { let db = new sqlite3.Database(dbPath); let sql = `SELECT number FROM ${tableName}`; db.run( `CREATE TABLE IF NOT EXISTS ${tableName}(name text, number text, address text)` ); db.all(sql, [], (err, rows) => { if (err) { return res.status(500).json(err); } else { return res.json(rows); } }); }); app.post('/add', (req, res) => { let db = new sqlite3.Database(dbPath); db.run( `INSERT INTO ${tableName}(name, number, address) VALUES(${req.name},${ req.number },${req.address})`, [], err => { if (err) return res.status(500).json(err); } ); return res.json({ msg: 'success' }); });

不管我在哪里发送多个回复。

回答如下:

每次你点击时我都不会创建你的数据库并创建你的表。

试试这个:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

你的/all路线也看起来有点偏。试试这个:

// get this out of the `/all` route, no need to initialize the db object over and over again
let db = new sqlite3.Database(dbPath);

// also leave this outside the `/all` route:
// no need to create the table over and over again.
db.run(
  `CREATE TABLE IF NOT EXISTS ${tableName}(name text, number text, address text)`
);


app.get('/all', (req, res) => {
  let sql = `SELECT number FROM ${tableName}`;

  // according to the sqlite3 api, the second parameter is optional, so just leave it out:
  db.all(sql, (err, rows) => {
    if (err) return res.status(500).json(err); // if you use return,  you don't need 'else' because the code will never reach it.

    res.json(rows)
  });
});
发布评论

评论列表 (0)

  1. 暂无评论