Answer the question
In order to leave comments, you need to log in
Why returns Failed to fetch?
POST https://localhost:5000/add-user/ net::ERR_CONNECTION_CLOSED
Uncaught (in promise) TypeError: Failed to fetch
const useDb = useDatabase();
<button onClick={() => useDb.addUser(userInfo.email, userInfo.name, userInfo.password)}>Submit</button>
export default function useDatabase() {
function addUser(email, name, password) {
let newUser = {
email: email,
name: name,
password: password
}
fetch(`https://localhost:5000/add-user/`, {
method: 'POST',
headers: {'Content-type': 'application/json'},
body: JSON.stringify(newUser)
}).then(data => console.log(data));
}
return {addUser}
}
const dotenv = require('dotenv');
const dbService = require('./dbService');
const cors = require('cors');
dotenv.config();
const express = require('express');
app = express();
app.use(cors());
app.use(express.json());
const port = process.env.SERVER_PORT;
app.listen(port, () => {console.log('server started on port ' + port)})
app.post('/add-user', function(req, res) {
console.log(req.body)
});
Answer the question
In order to leave comments, you need to log in
I suspect that the problem is in https and the request http://localhost:5000/add-user/
will work. For https, you need an ssl certificate, but in the server code I don’t see the creation of an https server.
While writing, you were moving in the same direction =).
Here's a bike:
function array_merge_sorted($first, $second)
{
$out = [];
for (
$f = 0, $s = 0,
$firstLength = count($first),
$secondLength = count($second);
$f < $firstLength || $s < $secondLength;
) {
if (!isset($first[$f])) {
$out[] = $second[$s];
++$s;
} elseif (!isset($second[$s])) {
$out[] = $first[$f];
++$f;
} elseif ($first[$f] > $second[$s]) {
$out[] = $second[$s];
++$s;
} else {
$out[] = $first[$f];
++$f;
}
}
return $out;
}
You are not like an array without re-sorting, because after merging, you will need to sort it again at the output.
Make a pass through the arrays, and insert values one after the desired values of the second, while constantly remembering the index of the value in another array in order to skip unnecessary iterations of the search for the insertion position. I think the idea is clear.
The correct solution is to apply the " Merge Sort " algorithm
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question