I have a stopwatch in react.js. After the stopwatch ends, the user can put their time in a database. In the index.js file, I call <App />
which is below, along with util.js.
App.js
import React from 'react';
import { addName } from "./util";
function App() {
const [name, setName] = React.useState("")
function handleUpdate(evt) {
setName(evt.target.value);
}
async function handleAddName(evt) {
await addName(name);
}
return <div>
<p><input type='text' value={name} onChange={handleUpdate} /></p>
<button className='button-style' onClick={handleAddName}>Add Name</button>
</div>
}
export default App;
util.js
import "isomorphic-fetch"
export function addName(name) {
return fetch('http://localhost:3001/addtime', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, time: Date.now() })
})
}
It puts their name and the time in the database, but they're weird times.
Each of these times was between 1 and 2 seconds on the stopwatch. I have the following in my node server.js file:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
...
app.post("/addtime", cors(), async (req,res) => {
const name = req.body.name;
const time = req.body.time;
// const timeStamp = dateFormat(time, dateFormat.masks.isoDateTime);
const template = 'INSERT INTO times (name,time) VALUES ($1,$2)';
const response = await pool.query(template, [name,time]);
res.json({name: name, time: time});
});
I can't understand why it is putting these weird numbers in the database
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…