Skip to content
Advertisement

Unable to push string into array

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

JavaScript

Error that I am getting when I run the code:

JavaScript

Advertisement

Answer

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 :

JavaScript

OR with .map() in one line :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement