slightly_toasted
slightly_toasted

Reputation: 335

Can't access value inside of props object

I am unable to access the value inside the year and month variables, as it returns an object.

getClassStatus () {
        var year = (this.props.year).toString();
        var month = (this.props.month).toString();
        var day = this.props.day;
        request
            .get('example.com/getRosterCount')
            .query({ year: {year} })
            .query({ month: {month} })
            .query({ day: {day} })
            .query({ time: 10 })
            .end((err, resp) => {
                if (!err) {
                    switch (resp.text) {
                        case "6":
                            this.setState({ status: 'full' });
                            break;
                        case "closed": 
                            this.setState({ status: 'closed' });
                            break;
                        case "booked":
                            this.setState({ status: 'booked' });
                            break;
                        default:
                            this.setState({ status: 'available' });
                            break;
                    }
                }
            });
    }

I have tried accessing the value as I would with any other object.

Eg.

var year = (this.props.year).toString(); // {year: '2019'}

console.log(year.year); // undefined

But it always returns undefined

How can I access this value?

Edit: Here's the server side code

app.get('/getRosterCount', (req, res) => {
    var year = req.query.year; // {year: '2019'}
    var month = req.query.month; // {month: '5'}
    var day = req.query.day;
    var time = req.query.time;

    console.log(year, month, day, time);

    // Add padding to month and day if needed so they always have 2 digits
    if (month.length == 1) {
        month = "0"+month;
    }

    if (day.length == 1) {
        day = "0"+day;
    }

    var dateString = year+'-'+month+'-'+day;

    pool.getConnection(function (err, connection) {
        if (err) throw err;
        connection.query("SELECT _fkUserID FROM roster WHERE (date = ? AND time LIKE ?) OR (date = ? AND time = ?)", 
        [
            dateString,
            time+'%',
            dateString,
            time
        ],
        function(error, rows, fields) {
            if (error) {
                console.log(error);
            }
            else {
                console.log(rows);
                console.log(rows.length);
                res.set('Content-Type', 'text/plain');
                res.send(rows.length.toString());
                console.log(this.sql);
            }
        });

        connection.release();
    });
});

The query never goes through because it ends up being this:

SELECT _fkUserID FROM roster WHERE (date = '[object Object]-[object Object]-[object Object]' AND time LIKE '10%') OR (date = '[object Object]-[object Object]-[object Object]' AND time = '10')

Upvotes: 0

Views: 238

Answers (2)

slightly_toasted
slightly_toasted

Reputation: 335

Fixed by removing the brackets around the variable.

Changed

   request
        .get('example.com/getRosterCount')
        .query({ year: {year} })
        .query({ month: {month} })
        .query({ day: {day} })

to

    request
        .get('example.com/getRosterCount')
        .query({ year: year })
        .query({ month: month })
        .query({ day: day })

Now I get the actual value in the URL rather than an object.

Upvotes: 0

SasoN
SasoN

Reputation: 150

If you call toString() you cant access the value because year is not an object anymore.

// obj.toString() === '[object Object]'

Try without toString().

Upvotes: 3

Related Questions