我已经设置了一条路由,通过该路由,我可以在firestore数据库的'users'集合下获取所有注册的电子邮件。在这条路线内,我正在渲染一个ejs模板,并希望显示所有电子邮件。
我的路线:
app.get('/check',(request,response)=>{
db.collection('users').get().then(snapshot=>{
snapshot.docs.forEach(doc=>{
response.render('check',{user: doc.data().email})
})
})
})
我的ejs模板:
<h1>check welcome</h1>
<ol>
<li><%= user %></li>
</ol>
模板被渲染,但是我只是注册了第一封电子邮件,而不是全部。在控制台中,我得到这个错误:
> Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
> at ServerResponse.setHeader (_http_outgoing.js:526:11)
> at ServerResponse.header (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/response.js:771:10)
> at ServerResponse.send (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/response.js:170:12)
> at done (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/response.js:1008:10)
> at tryHandleCache (/home/chahat/Desktop/firebase 2.0/functions/node_modules/ejs/lib/ejs.js:278:5)
> at View.exports.renderFile [as engine] (/home/chahat/Desktop/firebase 2.0/functions/node_modules/ejs/lib/ejs.js:478:10)
> at View.render (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/view.js:135:8)
> at tryRender (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/application.js:640:10)
> at Function.render (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/application.js:592:3)
> at ServerResponse.render (/home/chahat/Desktop/firebase 2.0/functions/node_modules/express/lib/response.js:1012:7)
我将不胜感激:)
您需要遍历文档,在Javascript中创建数据结构(可能是数组),然后将其传递给单个res.render()
,然后您的模板遍历数据以为每封电子邮件创建内容。每个请求只能呼叫res.render()
一次。