Postgres Rows Reflecting Added Property Always Display as Undefined

After retrieving objects from my postgres database, I am attempting to add a new property to each object. Strangely, when I log the entire object, the new property appears as expected. However, whenever I try to access or store this new property in another variable, it always returns undefined.

This is the snippet of code where I include the new property:

for(var i = 0; i < teams.rows.length; i++) {
    teams.rows[i].year = year
}
async.map(teams.rows, leagueResults, function(err, results) {
    console.log(results)
    cb(err, results)
})

Upon trying to retrieve the year property in leagueResults:

console.log(team)
team = "\'"+team.team_id+"\'"
var year = team.year
console.log('team', team, team.year)

The output for console logging team.year consistently shows "undefined". Has anybody encountered this issue before and found a solution? For reference, I am using the pg npm module to connect and query my postgres server.

Answer №1

When you are assigning a string to the variable team, you are losing the properties of the original team object.

To fix this issue, it is recommended to first save the value of team.year into a new variable called year, and then assign the new value to team. This way, you can retain the year value without affecting the properties of the team object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue: Incorrect parameters for executing the MySQL statement

Currently, I am working on a nodeJs project and utilizing the npm package mysql2 for connecting to a MySQL database. This is how my MySql Configuration looks like:- let mysql = MYSQL.createConnection({ host: `${config.mysql.host}`, user: `${config.mys ...

Having trouble with my Node.js/Express/Mongoose application where I am unable to save posts using the .save method after the first post. Could this be related to a

Utilizing AJAX, Node.js, Express, Mongoose, and MongoDB, the data is successfully delivered via ajax to node.js and logs properly. However, only the first post is being saved in MongoDB. When sending posts after the first one, all the new data is logged, ...

The app.html for the skygear/react-chat-demo is malfunctioning

I followed the instructions provided in the Skygear manual at https://docs.skygear.io/guides/advanced/server/ The skygear-server-darwin-amd64 started successfully. Then, I attempted to run the react-chat-demo project from https://github.com/skygear-de ...

The React app was successfully deployed to localhost:3000, but when attempting to access it, the message "This site can't be reached" is displayed. Strangely, the terminal confirms that the app is indeed

I've checked my code thoroughly, but when I deploy it to localhost, something seems off. Even after disabling all the components and running npm install, the issue persists. https://i.stack.imgur.com/i04eY.png Oddly enough, everything seems fine in ...

Struggling to successfully pass a function as an argument to the setTimeout method within an array in node.js using TypeScript

Here is an example that successfully demonstrates a function being called using setTimeout: function displayMessage(msg: string){ console.log(msg); } setTimeout(displayMessage, 1000, ["Hi!"]; After one second, it will print out "Hi!" to the console. ...

The Node version on Macbook is not compatible

After updating to the latest stable version of node, I noticed a few versions listed when checking with nvm. Here is what I did to update: sudo npm cache clean -f sudo npm install -g n sudo n stable nvm use v8.1.0 When I checked with nvm list, it displa ...

Encountering a TypeScript type error when returning a promise from a function

I currently have a scenario in which there is a function that checks if user whitelisting is required. If not, it calls the allowUserToLogin function. If yes, it then checks if a specific user is whitelisted. If the user is not whitelisted, an error is thr ...

Error encountered during the building of a Java project using Gradle

I ran into an issue with Git Bash error output (build failed). Despite attempting to resolve it by installing Python as suggested, setting the Python environment variable in IntelliJ, and following other recommendations, I still encounter the same build ...

The click() function is not properly functioning for the input tag in selenium automation when using nodejs

I am attempting to click on this specific element, but despite no errors, the click action is not being executed. <input class="btn-add-cart button js-form-submit form-submit" data-drupal-selector="edit-add" type="submit" i ...

Having trouble getting static serving to work on Express.js when custom middleware is added

Seeking assistance with creating a middleware for my express js website to incorporate subdomains and serve static image, css, and js files. While my HTML pages load properly, I encounter long page load times and the javascript files fail to load. Any gui ...

Mongoose Error: Incompatible receiver called Method Uint8Array.length

I am currently working on a small website that incorporates i18n. Initially, I used local json files, but upon transitioning to mongodb, I encountered an unfamiliar error. Any detailed explanation of this issue would be greatly appreciated. The specific e ...

Node/Express: Step-by-step guide on generating a file from a string and streaming it to the browser

Looking to achieve the following: 1. Extract a string from the request body and create an HTML file in memory, without writing it to disk. 2. Send this file that exists in memory as a downloadable attachment in the response. My current approach is not ...

Obtaining an Array through a direct input on the command line

I am having trouble incorporating raw command line arguments in my Node.js application. When I try with simple variables, everything works as expected (node example.js variable) However, when I pass an array as an argument, it does not work properly (n ...

Managing data from two tables in Node.js with ejs

I have a question regarding my node.js project that I need help with. As a beginner in this field, I believe the answer may be simpler than anticipated. In my code file named index.js, I found the following snippet after referring to some online documenta ...

Tips for displaying a view in Express while also sending a JSON object at the same time

I have encountered an issue that I need help solving. Currently, in ExpressJS 4, I am utilizing Jade as the template engine and AngularJS as the client-side framework. Through the mongoose module, I am retrieving a list of car brands from my MongoDB data ...

Access an object within a RowDataPacket with the use of Node.js and MySQL

The code in question: con.query(`SET @row_num = 0; SELECT @row_num := @row_num + 1 AS row_number, userid, username, lvl FROM users ORDER BY lvl + 0 DESC`, async (err, rowss) => { if(err) throw err; console.log(rowss[0].userid); }); ...

What is the location of the JSBin installation?

I came across this page while trying to set up JSBin locally. After successfully installing xcode 8.2 on my Mac, running npm -v now returns 3.10.9, and node -v returns v7.2.1. When I run npm install, the following output is generated: /Users/softtimur/St ...

Unable to get i18next functioning in my Node.js Express backend

I'm currently facing difficulties in implementing localization for my nodeJS backend. Within my Angular frontend, I have a language-setting interceptor that successfully sets the language in the request header. You can refer to the image below which ...

Node.js process.exec() function allows you to asynchronously spawn a subprocess

After writing the code, I ran it and found that the terminal was unresponsive with no output, causing the program to be stuck. var util=require('util') var exec=require('child_process').exec; exec('iostat 5',function(err,stdo ...

URL for a Heroku worker's HTTP endpoint

How can I initiate a worker process based on the payload received from an IronMQ message queue? IronMQ supports push queues, but they require an HTTP endpoint. Is there a way to set up an HTTP endpoint for a Heroku worker, or do I need to continuously pu ...