【发布时间】:2021-12-31 09:08:16
【问题描述】:
我正在尝试学习 EJS 并创建一个博客,但我似乎无法理解这个错误
我想做的是尝试将一些数据库响应作为对象写入数组,然后将其推送到文件中。 我正在使用复制数据库
const fs = require("fs")
const Database = require("@replit/database")
const db = new Database()
exports.load = async function(){
db.set("hello", {
"author": "Some author 1",
"title": "Blog Post 1",
"content": "First post content",
"date_posted": "Dec 17, 2021"
})
var posts = new Array()
db.list().then(keys => {
keys.forEach(key => {
posts.push(` <article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="/p">Anonymous</a>
<small class="text-muted">${db.get(key).date_posted}</small>
</div>
<h2><a class="article-title" href="#">${ db.get(key).title }</a></h2>
<p class="article-content">${ db.get(key).content }</p>
</div>
</article`
)
})
});
posts = posts.join()
fs.writeFileSync("public/posts.ejs", posts)
}
我在运行代码时遇到的错误:
UnhandledPromiseRejectionWarning: TypeError: posts.push is not a function
【问题讨论】:
标签: javascript node.js arrays ejs