简体   繁体   中英

How to Run Client-Side Function after Retrieving Data in a Node.js App

In a node.js Express app, I have this route:

router.get('/devices/:id/visualize', catchErrors(deviceController.visualizeDevice));

to this controller:

exports.visualizeDevice = async (req, res) => {
  const device = await Device
    .findOne( { _id: req.params.id } )
    .populate({
      path: 'states',
      options: { limit: 999, sort: { changed: 'asc' }}
    });
  confirmOwner(device, req.user);
  res.render('visualizeDevice', { title: `Visualize Device: ${device.name}`, device });
};

with the following view:

extends layout

block content
  -
    let statuses = device.states;
  //- pre= h.dump(statuses)
  .calendarContainer
    .calendar 
      .calendarHeader
        .yearHeader
        ul.calendarHeaderDays
          li Sat
          li Sun
          li Mon
          li Tue
          li Wed
          li Thu
          li Fri
        .arrow.top#upArrow △
        .days

If I dump statuses, I can see it's what I want; something like:

[
  {
    "changeType": "unlocked",
    "_id": "5c635f3dff415e0980ebbd1e",
    "author": "5b74a5b26513f70a28f8776a",
    "device": "5c635eb3ff415e0980ebbd19",
    "changed": "2019-02-10T18:00:00.000Z"
  },
  {
    "changeType": "unlocked",
    "_id": "5c6366f3194a4800155cfac1",
    "author": "5b74a5b26513f70a28f8776a",
    "device": "5c635eb3ff415e0980ebbd19",
    "changed": "2019-02-13T00:38:11.935Z",
    "__v": 0
  },
...
]

I want to run the following script on the client-side:

function populateCalendarDays(startDate=new Date()) {
  //  repeat 5 times for 5 weeks on the calender
  for (let j = 1; j <= 5; j++) {
    //  select the first day of the week
    let dayEl = document.querySelector(`.week${j}Day1`);
    //  repeat for 7 days
    for (let i = 0; i <= 6; i++) {
      if (getStatus(day) === "locked") {
        let lockEl = document.createElement('I');
        lockEl.classList.add('locked');
        dayEl.insertAdjacentElement('afterBegin', lockEl);
      }
      //  move to the next element
      dayEl = dayEl.nextElementSibling;
    }
    //  move to the next week
    startDate = addWeeks(startDate, 1);

  }
}

function getStatus(day) {
  status = "unlocked";
  for (let i = 0; i < statuses.length; i++) {
    if (compareAsc(day, parseISO(statuses[i].changed)) === 1) {
      status = statuses[i].changeType;
    }
  }
  return status;
}

but it gives me Uncaught ReferenceError: statuses is not defined . How should I write this so the front-end function can act on the data coming from the backend?

I don't know if it's the best way to do this, but you can pass the data to the front end in a script tag on the template.

script var statuses = !{JSON.stringify(statuses).replace(/<\//g, '<\\/')}

How to pass variable from jade template file to a script file?

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