admin管理员组文章数量:1415673
I have data in my database (MongoDB) and I am finding data from DB and saving it to array. And when button is clicked on page I want to send that data to my JavaScript file and using DOM show it on page.
I am finding data form DB when page loads:
app.get('/', function(req, res) {
var ipsumTextArray = [];
Ipsum.find({}, function(err, allIpsumTexts) {
if (err) {
console.log(err);
} else {
allIpsumTexts.forEach(function(ipsum) {
ipsumTextArray.push(ipsum.text);
});
}
res.render('home');
});
});
And in my other JavaScript file I want this function to get data from DB and do whatever I want.
function randomIpsum(text) {
text.value = 'text from database'; // text is textarea where I want to show text
}
I have data in my database (MongoDB) and I am finding data from DB and saving it to array. And when button is clicked on page I want to send that data to my JavaScript file and using DOM show it on page.
I am finding data form DB when page loads:
app.get('/', function(req, res) {
var ipsumTextArray = [];
Ipsum.find({}, function(err, allIpsumTexts) {
if (err) {
console.log(err);
} else {
allIpsumTexts.forEach(function(ipsum) {
ipsumTextArray.push(ipsum.text);
});
}
res.render('home');
});
});
And in my other JavaScript file I want this function to get data from DB and do whatever I want.
function randomIpsum(text) {
text.value = 'text from database'; // text is textarea where I want to show text
}
Share
Improve this question
edited Mar 27, 2020 at 16:15
jonrsharpe
122k30 gold badges268 silver badges476 bronze badges
asked Mar 27, 2020 at 16:14
TheRockTheRock
971 gold badge3 silver badges13 bronze badges
1
- You don't seem to be using the data when you render the home page. Is this a server-rendered or single-page application, even? Are you expecting the client to make requests (where are those requests) or the server to build the whole page (why not use the data)? – jonrsharpe Commented Mar 27, 2020 at 16:17
1 Answer
Reset to default 2You need to render with a parameter.
app.get('/', function(req, res) {
var ipsumTextArray = [];
Ipsum.find({}, function(err, allIpsumTexts) {
if (err) {
console.log(err);
} else {
allIpsumTexts.forEach(function(ipsum) {
ipsumTextArray.push(ipsum.text);
});
}
res.render('home', { arr: ipsumTextArray });
});
});
In the front-end (view):
var arr= {{ arr }}
function randomIpsum(text) {
//text.value = 'text from database'; // text is textarea where I want to show text
text.value = arr[0]
}
OR
You can send a plain text from your nodejs.
app.get('/', function(req, res) {
var ipsumTextArray = [];
Ipsum.find({}, function(err, allIpsumTexts) {
if (err) {
console.log(err);
} else {
allIpsumTexts.forEach(function(ipsum) {
ipsumTextArray.push(ipsum.text);
});
}
res.send(ipsumTextArray);
});
});
You can get the data using jQuery in the front-end.
<button id="btn">Get Data</button>
$("#btn").on("click", function(){
$.get("/", function(data){
randomIpsum(text, data)
})
})
function randomIpsum(text, data) {
//text.value = 'text from database'; // text is textarea where I want to show text
text.value = data
}
本文标签: nodejsPassing data from backend (nodejs) to frontend javascriptStack Overflow
版权声明:本文标题:node.js - Passing data from backend (nodejs) to frontend javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240169a2649279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论