为什么 Knex 将空值插入我的 table 数据库?

Why does Knex insert null values into my table database?

我有一个通过 express 连接的 SQLite 数据库,并且有控制器使用 express 路由连接前端和后端。

Table database

  return knex.schema.createTable('stocks', (table)  => {
          table.string('ticker')
          table.date('date')
          table.integer('open')
          table.integer('close')
          table.integer('high')
          table.integer('last')
          table.integer('volume')
        })
        .then(() => {
         
          console.log('Table \'stocks\' created')
        })
        .catch((error) => {
          console.error(`There was an error creating table: ${error}`)
        })
      }
    })
    .then(() => {
      // Log success message
      console.log('Ayree')
    })
    .catch((error) => {
      console.error(`There was an error setting up the database: ${error}`)
    })

Knex Insert Rows

exports.booksCreate = async (req, res) => {
  // Add new book to database
  knex('stocks')
    .insert({ // insert new record, a book
      "ticker": req.body.ticker,
      'date': req.body.date,
      'open': req.body.open,
      'close': req.body.close,
      'high': req.body.high,
      'last': req.body.last,
      'volume': req.body.volume,
    })
    .then(() => {
      // Send a success message in response
      res.json({ message: ` \'${req.body.ticker}\' added at ${req.body.date} created.` })
    })
    .catch(err => {
      // Send a error message in response
      res.json({ message: `There was an error creating ${req.body.ticker}  ${err}` })
    })
}

React Front End

// Create Bookshelf component
export const Bookshelf = () => {
  // Prepare states
   const [stock, setStock] = useState([])
   const [stockApi, setStockApi] = useState([])
  const [loading, setLoading] = useState(true)

  // Fetch all books on initial render
  useEffect(() => {
   
    getStockData();
    handleBookCreate();
  }, [])


  //StockData
const getStockData = ()  => { 
    axios
    .get("http://api.marketstack.com/v1/intraday/latest?access_key=72ad8c2f489983f30aaedd0181414b43&symbols=AAPL"       )
    .then( da => {
       setStock(da.data.data[0]) 
        console.log(da.data.data[0])
    } )
} 

// Create new book
  const handleBookCreate = () => {
    // Send POST request to 'books/create' endpoint
    axios
      .post('http://localhost:4001/books/create', { 
        ticker: stock.symbol,
        date: stock.date,
        open: stock.open,
        close: stock.close,
        high: stock.high,
        last: stock.last,
        volume: stock.volume,
      })
      .then(res => {
        console.log(res.data)

        // Fetch all books to refresh
        // the books on the bookshelf list
        fetchBooks()
      })
      .catch(error => console.error(`There was an error creating the ${stock} book: ${error}`))
  }

这里的问题出在前端,当我使用 stock.symbol 时,我得到一个空值,但是当我用“文本”替换 stock.symbol 时,我得到值文本。我在这里做错了什么?谢谢!

P.S - 这是常量库存

close: 115.08
date: "2020-10-09T00:00:00+0000"
exchange: "IEXG"
high: 116.4
last: 114.97
low: 114.5901
open: 116.25
symbol: "AAPL"
volume: 82322071
__proto__: Object

经过一段时间阅读 HTTP 调用及其执行方式。这个解决方案对我有用。

 useEffect( () => {

const fetchData = async ( ) => { 

  const getit =  await getStockData(); setStock(getit); 
    handleBookCreate(getit); 
    } 
         fetchData();

  }, [])

基本上在初始渲染 useEffect 上,进行 API 调用 getStockData() 并且等待将在承诺执行后设置库存 setStock 挂钩。只有这样 stock 才会包含 handleBookCreate() 向服务器路由器发送 POST 请求以执行在我的 table 数据库中插入记录的功能所需的数据。