简体   繁体   中英

Why is find function not recognized?

let students = ({id: 1, name: 'Alice'}, {id:2, name: 'Bob'}, {id: 3, name: 'Charlie' });
app.get('/',(req, res) => {
    //res.send('Home Page');
    //console.log(__dirname);
    //console.log(req.url);
    //console.log(req.query);
    res.sendFile('./views/index.html', {root: __dirname});
});


//send students with a particular ID back to the client
app.get('/students/:sid', (req, res) => {

    console.log(req.params);

    let id = req.params.sid;

    let student = students.find(element => element.id === parseInt(id));

    console.log(student);

    res.json(student);

});

in the console it says that the find function is not a function and is throwing me an error and I do not understand why.

An array is initialized with square brackets, the first line should look like this:

let students = [{id: 1, name: 'Alice'}, ...];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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