const { app,static } = require('../modules/app_config')
const { successAccess } = require('../modules/check_page')
const { pool } = require('../modules/mysql')
const { multer,bodyParser } = require('../modules/packages')
app.use(bodyParser.json({limit: '500mb'}));
app.use(bodyParser.urlencoded({limit: '500mb', extended: true}));
let images = []
let Storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, `${static}/images/`)
},
filename: (req, file, callback) => {
callback(null, Date.now() + "-" + file.originalname)
images.push( Date.now() + "-" + file.originalname)
}
})
let upload = multer({
storage: Storage
}).array("documents", 4)
app.get('/control-company',successAccess, (req,res) => {
res.sendFile(`${static}/www/control-company.html`)
})
app.post('/create-company',async (req,res) => {
console.log(req.body)
const {
name,
name2,
name3,
email,
skype,
website,
creator
} = req.body
await pool.query(`select * from users where email = '${creator}'`,async (error,results,fields) => {
console.log(`Creator: ${creator} / Result: ${results.map(r => r)}`)
const creator_id = results.map(r => r.id)
const documents = images.toString()
await pool.query(`INSERT INTO the_company SET ?`,{name,name2,name3,email,documents,skype,website,creator_id},(err,res) => {
if(err) console.log(err)
images = []
})
})
return res.redirect('/')
})
app.post('/upload', (req,res) => {
upload(req, res, function(err) {
if (err) {
return console.log("Something went wrong!")
}
console.log("File uploaded sucessfully!")
})
})
register.onsubmit = async (e) => {
e.preventDefault()
const form = new FormData(register)
fetch('/upload', {
method: 'POST',
body: form
})
fetch('/create-company', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body:
JSON.stringify({
name: form.get('name'),
name2: form.get('name2'),
name3: form.get('name3'),
email: form.get('email'),
skype: form.get('skype'),
website: form.get('website'),
creator: form.get('creator')
})
})
}
PayloadTooLargeError: request entity too large
async function sendImg(url) {
this.url = url
let data = new FormData()
let input = $('#file').val()
data.append('file', input.files[0])
data.append('name', 'hubot')
this.options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: data
}
const response = await fetch(this.url,this.options)
const res = await response.json()
}
$(document).on('click','.click', (e) => {
sendImg('/upload')
})
<input type="file" id="file" multiple><button class="click">Submit</button>
Uncaught (in promise) TypeError: Cannot read property '0' of undefined
at sendImg (index.js:15)
at HTMLButtonElement. (index.js:32)
at HTMLDocument.dispatch (jquery.min.js:2)
at HTMLDocument.v.handle (jquery.min.js:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,width=device-width,height=device-height,user-scalable=no">
<title>Menu</title>
<style type="text/css">
body {
color: #555;
}
.list {
background: #111;
}
.home {
display: none;
}
.lvl2 {
display: none;
}
.list:hover ~.lvl2 {
display: block;
}
</style>
</head>
<body>
<nav></nav>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
async function makeMenuLevel() {
const arr = await fetchTodos('/menu')
let ul = '<ul>';
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
ul += '<li class="list">' + item.title + '</li>';
for (let j = 0; j < item.children.length; j++) {
let child = item.children[j];
if (j === 0) {
ul += '<ul class="lvl2">';
}
ul += '<li class="list">' + child.title + '</li>';
if (child.children) {
for (let k = 0; k < child.children.length; k++) {
let child2 = child.children[k];
if (k === 0) {
ul += '<ul class="home">';
}
ul += '<li>' + child2.title + '</li>';
if (child2.children) {
for (let n = 0; n < child2.children.length; n++) {
let child3 = child2.children[n];
if (n === 0) {
ul += '<ul>';
}
ul += '<li>' + child3.title + '</li>';
if (n === child2.children.length - 1) {
ul += '</ul>';
}
}
}
if (k === child.children.length - 1) {
ul += '</ul>';
}
}
}
if (j === item.children.length - 1) {
ul += '</ul>';
}
}
}
ul += '</ul>';
console.log(ul)
document.body.innerHTML = ul;
}
makeMenuLevel()
async function fetchTodos(url) {
this.url = url
this.options = {
method: 'POST'
}
console.log('Fetch todo started...')
const response = await fetch(this.url,this.options)
const data = await response.json()
console.log(data)
return data
}
</script>
</body>
</html>