简体   繁体   English

如何在Node.js和Koa2的后端调用函数

[英]How to call the function in backend in Node.js & koa2

I use Node.js, koa2 and ejs to build a website, and there's a function like following 我使用Node.js,koa2和ejs来构建网站,并且有如下功能

in app.js 在app.js中

mysqOptlModel.getAllRecords()
.then(async(result) => {
   await ctx.render('AllRecordsView', {
       allrecords:result
   })
})

in list.ejs of the frontend 在前端的list.ejs

<% allrecords.forEach(function(item, i){%>
<tr>
  <td><%=item.username%></td>
  <td><%=item.createdtime%></td>
</tr>
<%})%>

Here are my questions 这是我的问题

  1. I don't want to show the full username, I just want to show some parts, and some is instead by * 我不想显示完整的用户名,我只想显示一些部分,而用*代替

  2. The createdtime is timestamp format, and I want to change it to Date format createdtime是时间戳格式,我想将其更改为Date格式

My current solution 我目前的解决方案

I write two JavaScript function in list.ejs , and call them to use. 我在list.ejs编写了两个JavaScript函数,并调用它们来使用。

My expectation 我的期望

I want to do this in app.js , not in list.ejs 我想在app.js执行此操作,而不是在list.ejs中执行此操作

Is there any ideas? 有什么想法吗?

This could be a way to modify your result in app.js : 这可能是在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM