简体   繁体   中英

How to access a key using the dot operator on an object in which the key begins with :_ in javascript?

I am doing a beginners' Nodejs API project I am required to post to API endpoint /api/users/:_id/exercises and when I get req.body, it has the following form wehen I do a console.log():

 { ':_id': '61d0c0b2fa1547d5c637e03c', description: 'Pusups', duration: '25', date: '2021-12-29' }

Now, I am trying to get the values of these objects by using their keys and a dot operator but I can't find a way to get the key of the object that has the value 61d0c0b2fa1547d5c637e03c. I can access req.body.description and req.body.duration and req.body,date but I can not access req.body.:_id.

I tried the follwoing to practice and find a way to access they first property in this object but I couldn't.

 const object1 = { ':_id': '61d0c0b2fa1547d5c637e03c', description: 'Pusups', duration: '25', date: '2021-12-29' } let someArray = Object.keys(object1); console.log(object1.description); console.log(object1.duration); console.log(object1.date); const someVariable = someArray[0].substring(0, 4); console.log(someVariable); console.log(object1.someVariable); console.log(object1._id);

Pusups 25 2021-12-29:_id undefined undefined

When I try to post to API endpoint /api/users/:_id/exercises, the post attempt fails. Here is my post function:

 app.post('/api/users/:_id/exercises', async function(req, res, next) { console.log(req.body); if (req.body) { //new mongoose.Types.ObjectId().toHexString() const id = mongoose.Types.ObjectId(body.':_id'); try { const newExercise = await addNewExercise( { _id: id, description: req.body.description, duration: req.body.duration, date: req.body.date? req.body.date: null } ); console.log(newExercise); res.json( { username: newExercise.username, description: newExercise.description, duration: newExercise.duration, date: newExercise.date, _id: newExercise._id } ); } catch (e) { console.error(e); } } });

Here is link to my code on replit

Please help!

You can it by doing

 const id = req.body[':_id']

Here's how you can access it:

console.log(object1[":_id"]);

I don't think it is possible to do it using dot operator but I don't think is a problem as long as you can access the field.

You're misunderstanding the intention of this line:

app.post('/api/users/:_id/exercises', async function(req, res, next) {

This is an example of Express route parameters . From the Express documentation:

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.

Instead of making requests to the literal endpoint /api/users/:_id/exercises , you would make a request to something like /api/users/<someUserId>/exercises , where someUserId is a valid ObjectId. You would then retrieve this value in your code with req.params._id .

How using the Object.values() method?

let firstValue= Object.values(object1)[0];
let secondValue= Object.values(object1)[1];

console.log(firstValue); //logs 61d0c0b2fa1547d5c637e03c
console.log(secondValue);//logs pusups

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