简体   繁体   English

无法访问props对象内部的值

[英]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 但是它总是返回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')

If you call toString() you cant access the value because year is not an object anymore. 如果调用toString() ,则无法访问该值,因为year不再是对象。

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

Try without toString() . 尝试不使用toString()

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. 现在,我在URL中获得了实际值,而不是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM