1

I have created a get route for a page which displays user info. In this I have created a list array which I have passed to render object and this list contains 2 things one is user image which is displaying correctly and the other is his attendance in form of an array which includes different objects.

The problem is when I am pushing this objects they are not getting added to the dates array and are not getting displayed in the list array. Also when I am console logging the objects they are getting displayed individually.

router.get("/me", ensureAuthenticated, async (req, res) => {
    var empCode = req.user.empCode;
    var list = []
    list.photo = await Document.findOne({
        empCode: empCode
    }).then(employee => {
        return employee.photo;
    });


    list.attendance = await Attendance.findOne({
        empCode: empCode
    }).then(employee => {    
        var dates = [];    
        var emp = {};
        employee.years.forEach(year => {
            yearName = year.name;
            emp["year"] = yearName;
            year.months.forEach(month => {
                monthName = month.name;
                emp["month"] = monthName;
                month.days.forEach(day => {
                    date = day.date;
                    inTime = day.inTime;
                    outTime = day.outTime;
                    status = day.status;
                    emp["date"] = date;
                    emp["inTime"] = inTime;
                    emp["outTime"] = outTime;
                    emp["status"] = status;
                    dates.push(emp); // Here is i console log the objects are getting displayed.
                });                
            });            
        });
        return dates;
    });

    console.log(list);
    res.render("me", {
        user: req.user,
        list
    });
});

If I console.log(emp); in forEach loop this is the result.

{
  year: '2020',
  month: 'Jan',
  date: '1',
  inTime: '09:40',
  outTime: '18:10',
  status: 'On Time'
}
{
  year: '2020',
  month: 'Feb',
  date: '3',
  inTime: '10:15',
  outTime: '18:15',
  status: 'Late'
}
{
  year: '2020',
  month: 'Feb',
  date: '4',
  inTime: '10:20',
  outTime: '18:20',
  status: 'Late'
}
[ photo: '', attendance: [] ]

But if I do dates.push(emp); then I get.

[
  photo: '',
  attendance: [
    {
      year: '2020',
      month: 'Feb',
      date: '4',
      inTime: '10:20',
      outTime: '18:20',
      status: 'Late'
    },
    {
      year: '2020',
      month: 'Feb',
      date: '4',
      inTime: '10:20',
      outTime: '18:20',
      status: 'Late'
    },
    {
      year: '2020',
      month: 'Feb',
      date: '4',
      inTime: '10:20',
      outTime: '18:20',
      status: 'Late'
    }
  ]
]

And what I want is this.

[
  photo: '',
  attendance: [
    {
  year: '2020',
  month: 'Jan',
  date: '1',
  inTime: '09:40',
  outTime: '18:10',
  status: 'On Time'
}
{
  year: '2020',
  month: 'Feb',
  date: '3',
  inTime: '10:15',
  outTime: '18:15',
  status: 'Late'
}
{
  year: '2020',
  month: 'Feb',
  date: '4',
  inTime: '10:20',
  outTime: '18:20',
  status: 'Late'
}
  ]
]
4
  • 1
    What error you are getting? Commented Feb 5, 2020 at 5:30
  • this code should display 3 objects in attendance array but instead it displays last object 3 times. Commented Feb 5, 2020 at 5:42
  • Could you copy and paste your expected output and your current output into your original post? Also to clarify, you're saying you're getting the correct output when you log emp while in the forEach loop? Commented Feb 5, 2020 at 5:43
  • please check i have updated the question with the results i am getting and what is want Commented Feb 5, 2020 at 6:06

1 Answer 1

2

You should convert your variable emp to a new instance of object. you should push to array like this.

dates.push(JSON.parse(JSON.stringify(emp));

This will convert your variable emp into a new instance of object without any connection to the object you were accessing during your iteration.

Variables that are assigned a non-primitive value are given a reference to that value. That reference points to the object’s location in memory. The variables don’t actually contain the value.

Primitives vs. References Please read this docs to understand further value vs reference

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

1 Comment

hi i am stuck after this step too can you please help this is the post. stackoverflow.com/questions/60073924/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.