Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.6 kB
4
Indexable
Never
app.get('/api/sdh/de-tai/page/:pageNumber/:pageSize', app.permission.check('sdhDeTai:read'), async (req, res) => {
    try {
      const { pageNumber, pageSize } = req.params;
      let { filter } = req.query || {};
      filter = filter || {};

      const { rows: list, pagenumber, pagesize, pagetotal, totalitem } = await app.model.sdhDeTai.searchPage(Number(pageNumber), Number(pageSize), app.utils.stringify(filter));

      list.forEach(item => {
        if (item.canBo) {
          console.log('Original canBo:', item.canBo);  // Log the original string
          try {
            // Clean the string by removing extra quotes around values
            let cleanedString = item.canBo
              .replace(/\\"/g, '"')  // Remove backslashes before quotes
              .replace(/""/g, '"');  // Replace double double-quotes with a single double-quote

            console.log('Cleaned canBo:', cleanedString);  // Log the cleaned string

            // Parse the JSON string
            item.canBo = JSON.parse(cleanedString);
          } catch (err) {
            console.error('Error parsing canBo JSON:', err);
            console.error('Problematic JSON string:', item.canBo);
            item.canBo = []; // Set to empty array if parsing fails
          }
        } else {
          item.canBo = []; // Set to empty array if canBo is null or undefined
        }
      });

      res.send({ page: { list, pageNumber: pagenumber, pageSize: pagesize, pageTotal: pagetotal, totalItem: totalitem } });
    } catch (error) {
      app.consoleError(error);
      res.send({ error });
    }
  });
Leave a Comment