Home > Software engineering >  Unable to push string into array
Unable to push string into array

Time:12-31

I am trying to learn EJS and make a blog but I cant seem to understand this error

What I am trying to do is try to write some db response as an Object to an array then push it to the file. I am using replit DB

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 >
        <div >
          <div >
            <a  href="/p">Anonymous</a>
            <small >${db.get(key).date_posted}</small>
          </div>
          <h2><a  href="#">${ db.get(key).title }</a></h2>
          <p >${ db.get(key).content }</p>
        </div>
      </article`
      )
    })
  });

  posts = posts.join()

  fs.writeFileSync("public/posts.ejs", posts)
}

Error that I am getting when I run the code:

UnhandledPromiseRejectionWarning: TypeError: posts.push is not a function

CodePudding user response:

First, you declare var posts = new Array(). So posts is an array. Next line (in execution order) : posts = posts.join(). So now posts is an empty string. You are changing the type of the variable, which is a bad practice (Typescript wouldn't let you do that). Now next line in execution order : .then(keys =>. You start pushing stuff into posts, but posts is now a string, remember? Not an array anymore.

You use the async keyword for no reason, since there is no await in it. You might as well leverage it :

exports.load = async function(){
  db.set("hello", {
    "author": "Some author 1",
    "title": "Blog Post 1",
    "content": "First post content",
    "date_posted": "Dec 17, 2021"
  })

  let postsArray = new Array();

  const keys = await db.list();

  keys.forEach(key => {
    postsArray.push(`<article >
      <div >
        <div >
          <a  href="/p">Anonymous</a>
          <small >${db.get(key).date_posted}</small>
        </div>
        <h2><a  href="#">${ db.get(key).title }</a></h2>
        <p >${ db.get(key).content }</p>
      </div>
    </article`
    )
  })
  
  const posts = postsArray.join()

  fs.writeFileSync("public/posts.ejs", posts)
}

OR with .map() in one line :

exports.load = async function(){
  db.set("hello", {
    "author": "Some author 1",
    "title": "Blog Post 1",
    "content": "First post content",
    "date_posted": "Dec 17, 2021"
  })

  const keys = await db.list();

  const posts = keys.map( key => `<article >....</article`).join();

  fs.writeFileSync("public/posts.ejs", posts)
}
  • Related