I have this clunky code here and hope there is any way to shorten this. By the way, "req.query" is my query object from an nodejs express server.
const q = req.query
User.insertOne({
firstName: q.firstName,
lastName: q.lastName,
bestFriend: q.bestFriend,
hobbies: q.hobbies,
address: {
street: q.address.street,
number: q.address.number,
plz: q.address.plz,
city: q.address.city,
country: q.address.country,
},
contact: {
telephone: q.contact.telephone,
email: q.contact.email,
},
})
My first solution was - as you can see - to replace req.query with q, but this does not seem to be enough.
Maybe you could do this with the "with" statement, but i haven't used it before and I've heard that you shouldn't implement it (don't know why....).
CodePudding user response:
As long as the property names matches with each other, you can directly assign them in javascript and the relevant properties will be mapped.
const q = req.query
User.insertOne(q);
If the properties don't match, use spread operator ( ... ) from ES6 which comes handy while mapping objects.
CodePudding user response:
You can use the lodash library and use a the pick method.You only need to specify the names of the fields that you want from req.query,and it'll return an object containing those values which you can use.
const _ = require("lodash");
User.insertOne(_.pick(req.query, [
"firstName",
"lastName",
"bestFriend",
"hobbies",
"address.street",
"address.number",
"address.plz",
"address.city",
"address.country",
"contact.telephone",
"contact.email"
]))
CodePudding user response:
By reading your title I understood you want to use ES6 object property shorthand. To achieve that in your current setup you would also need to use object destructuring, here's the code:
//Object destructuring:
const { firstName, lastName, bestFriend, hobbies } = req.query;
const { street, number, plz, city, country } = req.query.address;
const { telephone, email } = req.query.contact;
//Using the ES6 object property shorthand:
User.insertOne({
firstName,
lastName,
bestFriend,
hobbies,
address: {
street,
number,
plz,
city,
country,
},
contact: {
telephone,
email,
},
})
CodePudding user response:
In case the OP's task was about passing just a subset of the queried data into the method (but actually not just for that) then the OP might have a look into the destructuring assignment syntax, into the spread syntax as well as into the rest parameters syntax.
const req = {
query: {
foo: 'Foo',
bar: 'Bar',
firstName: 'Anni',
lastName: 'Smith',
bestFriend: 'Jane Doe',
hobbies: '',
address: {
street: 'Any Lane',
number: 42,
plz: '1-234-456',
city: 'Some City',
country: 'Motherland',
},
contact: {
telephone: '9-87-654-321',
email: '[email protected]',
},
more_not_needed_data: {
baz: 'Baz',
biz: 'Biz',
},
},
};
const userDataFromRest =
(({ foo, bar, more_not_needed_data, ...data }) => data)(req.query);
const explicitUserData = (
({
firstName, lastName, bestFriend, hobbies, address, contact,
}) => ({
firstName, lastName, bestFriend, hobbies, address, contact,
})
)(req.query);
/*
User
.insertOne((({ foo, bar, 'more not needed data', ...data }) => data)(req.query));
User
.insertOne((
({
firstName, lastName, bestFriend, hobbies, address, contact,
}) => ({
firstName, lastName, bestFriend, hobbies, address, contact,
})
)(req.query));
*/
console.log({
query: req.query,
userDataFromRest,
explicitUserData,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }