0

I am trying to add the quoteValue key-value in an element (an object in this case) of the users array using the code below.

When I print out console.log(users[0]), it's not showing the value of quoteValue for users[0]. However, console.log(users[0].quoteValue) prints the actual value of quoteValue.

I don't understand how it is possible. It would really appreciate your help!

export async function get_client_users(req, res) { try { let users = await User.find({ role: { $eq: 'client' }, status: { $ne: 'deleted' } }, { name: 1, mobile: 1, email: 1, status: 1, ref_id : 1, _id: 1 }); for(let i = 0; i < users.length; i += 1) { let quotes = await Quote.find({client: users[i]._id}); const totalQuote = quotes.length; let cost = 0; for(let i = 0; i < quotes.length; i += 1) { cost += quotes[i].total_cost; } const result = { totalQuote: totalQuote, quoteValue: cost } Object.assign(users[i], result); } return res.status(200).json(users); } catch(e) { console.log(e); return res.status(400).json({ message: 'Technical Error. Please try again later.' }); }; };

8
  • 1
    What are you trying to accomplish with Object.assign(users[i], result);? What do you want that array element to look like? And, is there any reason you don't just do users[i].totalQuote = totalQuote; and users[i].quoteValue = cost;? Commented Dec 12, 2019 at 18:51
  • 1
    Where is it that console.log('user =>', users[0]) doesn't work? Where did you put that line of code? It's possible that some properties are configured enumerable: false and thus won't show in console.log(users[0]), but will show in console.log(users[0].property). You can test that for sure with Object.getOwnPropertyNames(users[0]) to see every property that's actually there regardless of enumerability. Commented Dec 12, 2019 at 18:54
  • I am trying to send the quoteValue and totalQuote with users in response, how I can send this Commented Dec 12, 2019 at 19:07
  • Hi Gaurav, I've edited your post to help others understand your question. Hope it helps! Commented Dec 12, 2019 at 19:09
  • I want to attach this key value pair({totalQuote: totalQuote, quoteValue: cost}) with users and want to send in response Commented Dec 12, 2019 at 19:11

1 Answer 1

1

I would recommend using destructing (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) if you can to create a new users object (called updatedUsers in the code below) like so:

export async function get_client_users(req, res) { try { let users = await User.find({ role: { $eq: 'client' }, status: { $ne: 'deleted' } }, { name: 1, mobile: 1, email: 1, status: 1, ref_id : 1, _id: 1 }); let updatedUsers = []; for(let i = 0; i < users.length; i++) { let quotes = await Quote.find({client: users[i]._id}); let quoteValue = 0; for(let i = 0; i < quotes.length; i++) { quoteValue += quotes[i].total_cost; } updatedUser = { ...users[i], totalQuote: quotes.length, quoteValue } updatedUsers.push(updatedUser); } return res.status(200).json(updatedUsers); } catch(e) { console.log(e); return res.status(500).json({ message: 'An error occurred. Please try again later.' }); }; }; 

I also changed a few things like sending 500 instead of 400 when an error occurs, removed the assignment to the totalQuote variable by assigning quotes.length directly to updatedUser.totalQuote, and also used i++ instead of i += 1 in your for loops. I would recommend the usage of a linter such as ESLint (https://eslint.org/) or Prettier (https://prettier.io/) to improve the readability of your code.

Additionally, I would suggest to use map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) to iterate over your users object and reduce (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) to get the value of quoteValue from the total_cost property of your quotes, but this is outside the scope of your question.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.