The user is defined, but the user's user ID is not specified

It seems that the user is defined, but user.user_id is not. My framework of choice is express.js and passport.js.

router.post('/requestSale', function(req,res){
    console.log('session user: ' +  req.session.passport.user); //logs 
    console.log('session user_id: ' + req.session.passport.user.user_id);
    api.initiateSale(req.body.brokerId, req.session.passport.user.user_id, (req.body.amount).toFixed(0), function(sale){
        res.render('buying', {title: 'Buying', sale:sale});
    });    
});

console.log('session user: ' +  req.session.passport.user);
logs:

{"user_id":3,"type":"Normal","email":"[email protected]","firstname":"Kinnard","lastname":"Hockenhull"}`

However,

console.log('session user_id: ' +  req.session.passport.user.user_id);
logs:

undefined

Why is this happening and how can I resolve it?

Answer №1

req.session.passport.user appears to be stored as a JSON string rather than a JavaScript object. This is why it does not have the user_id property and returns undefined. To access the property, you will need to parse the JSON using the JSON.parse method.

JSON.parse(req.session.passport.user).user_id;

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

Looping through an array

I have created an array as shown below: iArray = [true, true, false, false, false, false, false, false, true, true, true, false, true, false, false, false, false, true] Condition check: If any value in this array is false, I will display an error messag ...

Issues arise when running shell scripts on Linux due to Node.js providing unwanted messages during execution

Attempting to run a shell script using Node.js, but the Node.js server is displaying incorrect error messages. The shell script: sudo mkdir updateinprogress servicebranch="Development" currentpath="$PWD" tarfilename="$(date +'%d-%m-%Y_%H-%M-%S&apos ...

I encountered a difficulty trying to assign a value to @Input decorators in the spec file

While writing a test case for form submission, I encountered an issue where the Input decorator (e.g. station) value is reassigned during form submission. However, when attempting to define this Input value in the spec file, the component.station value is ...

Tips for updating server-side variables from the client-side in Next.js

There is a code snippet in api/scraper.js file that I need help with. const request = require("request-promise"); const cheerio = require("cheerio"); let url = "https://crese.org/distintivo-azul/"; let result; request(url, ...

The elements being parsed are appearing as undefined

Currently, I am attempting to analyze this JSON structure: { "customers": [ { "name":"joe" , "cars":[ {"name":"honda","visits":[ {"date":"01/30/14","Id":"201"}, {"date":"01/30/14","Id":"201"}, {"date":"02/12/14","Id":"109"} ...

What is the optimal approach for managing multiple languages using React Router version 5?

I am exploring the possibility of incorporating multiple languages into my website using React and React Router v5. Can you provide guidance on the most effective approach to achieve this? Below is a snippet of the current routing code I am working with: ...

Slide containing Ionic list views

In my Ionic app, I am using ion-slide-box with 3 slides. Each slide contains an ion-list (table view) with varying numbers of items. The issue is that when scrolling through the shorter list items, it shows empty content due to the taller sibling list taki ...

Step by step guide on manually signing a cookie with cookieParser

In order to test my Express app, I need to include a signed cookie in the HTTP request. This way, the server can recognize it as a signed cookie and place it in the req.signedCookies object. However, I have not found a suitable method for this in the docu ...

The Ion-button seems to be malfunctioning

I am interested in using special buttons for my ionic 1 project, specifically the ion-button feature outlined on this page: Ionic Buttons. I attempted to create a Round Button and an Outline + Round Button: <h2 class="sub-header" style="color:#4 ...

Unexpected disappearance of form control in reactive form when using a filter pipe

Here is a reactive form with an array of checkboxes used as a filter. An error occurs on page render. Cannot find control with path: 'accountsArray -> 555' The filter works well, but the error appears when removing any character from the fi ...

When attempting to access the Object data, it is returning as undefined, yet the keys are being

I have an Object with values in the following format: [ { NameSpace: 'furuuqu', LocalName: 'uuurur', ExtensionValues: 0, FreeText: 'OEN', '$$hashKey': 'object:291' }, { Nam ...

NodeJs Importing a File

Currently working with NodeJS, I have encountered a challenge. Is it possible to require a JavaScript file in Node similar to how we do in browsers? When using the require() method, I noticed that the JavaScript file called does not have access to global v ...

Alter the URL and CSS upon clicking an element

Currently, I am faced with a challenge on my website where I have multiple pages that utilize PHP to include content and jQuery to toggle the display of said content by adjusting CSS properties. However, I am encountering difficulty in implementing this ...

Disable the ability to select text when double-clicking

Is there a way to prevent text selection on double click while still allowing selection on mouse drag? Whenever I try to remove selection on dblclick or mouseup, it flashes, which is not the desired outcome as shown in this jsfiddle. UPD: I am not lookin ...

AngularJS - Refreshing Controller when Route Changes

Scenario app.controller('headerController', ['$scope', '$routeParams', function($scope, $routeParams) { $scope.data = $routeParams; }]); app.config(['$routeProvider', function ($routeProvider) { ...

Exploring X3DOM nodes using d3.js

I'm attempting to loop through X3DOM nodes in D3.js, but I'm encountering an issue. Check out the code snippet below: var disktransform = scene.selectAll('.disktransform'); var shape = disktransform .datum(slices ...

Despite setting up express.static, Express.js is still unable to access static files

I attempted to access localhost:3000/index.html (where a static page is located), localhost:3000/javascripts/dio.js (where the JavaScript file is stored), and localhost:3000/images/dio1.jpg (where the images are housed). However, none of these resources ca ...

When the browser is closed, Express and Redis sessions are unable to persist

Despite setting the maxAge and ttl values in RedisStore for persistent sessions, my session gets destroyed whenever I close the browser. I'm unsure of what mistake I might be making. Any insights on why the session doesn't survive a browser resta ...

JavaScript - Capture the Values of Input Fields Upon Enter Key Press

Here's the input I have: <input class="form-control" id="unique-ar-array" type="text" name="unique-ar-array" value="" placeholder="Enter a keyword and press return to add items to the array"> And this is my JavaScript code: var uniqueRowsArr ...

Using ExpressJS for OAuth2 with passport and oauth2orize

Just diving into the world of expressJS for the first time ;) Currently working on setting up an oauth2 server (password workflow) using passport and oauth2orize. Testing the connection using httpie on my Mac. Server console output: info: [bin/www] Lis ...