[英]How to call the function in backend in Node.js & koa2
我使用Node.js,koa2和ejs来构建网站,并且有如下功能
在app.js中
mysqOptlModel.getAllRecords()
.then(async(result) => {
await ctx.render('AllRecordsView', {
allrecords:result
})
})
在前端的list.ejs
中
<% allrecords.forEach(function(item, i){%>
<tr>
<td><%=item.username%></td>
<td><%=item.createdtime%></td>
</tr>
<%})%>
这是我的问题
我不想显示完整的用户名,我只想显示一些部分,而用*代替
createdtime是时间戳格式,我想将其更改为Date格式
我目前的解决方案
我在list.ejs
编写了两个JavaScript函数,并调用它们来使用。
我的期望
我想在app.js
执行此操作,而不是在list.ejs中执行此操作
有什么想法吗?
这可能是在app.js
修改结果的一种方式:
mysqOptlModel.getAllRecords()
.then(async(result) => {
// here the modification happens
modifiedResult = result.map((item) => {
return {
username: item.username.substr(0,3) + '*****',
date: new Date(item.createdtime).toISOString().substr(0,10)
}
})
await ctx.render('AllRecordsView', {
allrecords:modifiedResult // pass the modified result
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.