1 回答
TA贡献1777条经验 获得超10个赞
您需要使用参数进行渲染。
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 });
});
});
在前端(视图):
var arr= {{ arr }}
function randomIpsum(text) {
//text.value = 'text from database'; // text is textarea where I want to show text
text.value = arr[0]
}
或者
您可以从 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);
});
});
您可以在前端使用 jQuery 获取数据。
<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
}
添加回答
举报
