英文:
Re-"res.render" URL after "fetch POST"-ing DOM data array to Node-js
问题
我明白,你想要的是代码部分的翻译。以下是翻译后的代码:
// 摘录的渲染代码 ////////////////////////////////////////////////
function render(file, url, contentdata) {
app.get(url, async(_req, res, next) => {
try {
res.render(file, { data: await contentdata });
} catch (err) {
next(err);
}
});
}
render('time-picker', '/time-picker', timetablesthree)
// 摘录的新TIME-PICKER表格到数据库的部分
app.post("/time-picker-input", async(req, res) => {
for(i=0; i<=167; i++) {
let inputFieldId = req.body[i].fieldid;
let inputFieldValue = req.body[i].fieldvalue;
// console.log(inputFieldId);
await ttupdate(inputFieldValue, inputFieldId, 'timetablesthree');
};
res.json({ message: 'Success' });
});
如果你还需要其它翻译,请继续提供。
英文:
Even after reading many related questions, I am uncertain as to what exactly is the best approach to render a URL after JS frontend collects DOM data and fetch POSTs it to an express server POST URL.
- I am aware of the fact that fetch POST does not ask for rendering
- at this point I am not asking for an alternative POST method (XML / FORM etc.). I would ask a seperate question if this current approach is not valid
- I am thankful for replies on how to instantly "refresh" the URL as well, this is however predominantly asking about having the URL rendered for the next browser GET request
- I am "presuming" to understand that my app only renders the URL once at server start and that this is the problem
How can I call the function render after the fetch POST and the MYSQL query "ttupdate" have completed?
// snippet render code ////////////////////////////////////////////////
function render(file, url, contentdata) {
app.get(url, async(_req, res, next) => {
try {
res.render(file, { data: await contentdata });
} catch (err) {
next(err);
}
});
}
render('time-picker', '/time-picker', timetablesthree)
Where and how in the following snippet would you implement it?
// snippet new TIME-PICKER table to DB
app.post("/time-picker-input", async(req, res) => {
for(i=0; i<=167; i++) {
let inputFieldId = req.body[i].fieldid;
let inputFieldValue = req.body[i].fieldvalue;
// console.log(inputFieldId);
await ttupdate(inputFieldValue, inputFieldId, 'timetablesthree');
};
res.json({ message: 'Success' });
});
Additional information on my setup:
let express = require('express')
let bodyParser = require('body-parser')
let app = express()
app.set('view engine', 'ejs')
let path = require('path')
app.set('views', path.join(__dirname, 'views'))
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Sorry - something broke...')
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
let { singledbentry, refresh, refreshTwo, ttupdate } = require("./database.js")
let activetime = require('./activetime.js');
let mysql = require('mysql2')
答案1
得分: 0
以下是您要翻译的内容:
"Background: A lot of endpoints rendered correctly and also rendered correctly with updated data after a FORM action POST.
The only endpoint not rendering properly, unless after server restart, was using fetch POST. So I was mislead to believing the obvious cause to be the different behavior of fetch POST (like not requesting rendering like a browser).
However none of the endpoints rendered correctly after reloads. This was just not visible, as they always displayed the correct data through express POST res.render.
Edit:
For future readers: I took the route out of the function and passed the information for "timetablesthree" directly to prove that is was an issue of variable scope.
The following code works fine:
app.get('/time-picker', async(_req, res, next) => {
try {
timetablesthree = refresh('SELECT ttfield, ttfieldvalue FROM timetablesthree')
res.render('time-picker', { data: await timetablesthree });
console.log(timetablesthree);
} catch (err) {
next(err);
}
});"
英文:
It was a matter of variable scope.
Background: A lot of endpoints rendered correctly and also rendered correctly with updated data after a FORM action POST.
The only endpoint not rendering properly, unless after server restart, was using fetch POST. So I was mislead to believing the obvious cause to be the different behavior of fetch POST (like not requesting rendering like a browser).
However none of the endpoints rendered correctly after reloads. This was just not visible, as they always displayed the correct data through express POST res.render.
Edit:
For future readers: I took the route out of the function and passed the information for "timetablesthree" directly to prove that is was an issue of variable scope.
The following code works fine:
app.get('/time-picker', async(_req, res, next) => {
try {
timetablesthree = refresh('SELECT ttfield, ttfieldvalue FROM timetablesthree')
res.render('time-picker', { data: await timetablesthree });
console.log(timetablesthree);
} catch (err) {
next(err);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论