Ad

Substring String In Nodejs And Pug

- 1 answer

I have a search pug file with this content

extends layout.pug

block css
  link(rel="stylesheet", target="_blank" rel="nofollow noreferrer" href="/css/article_search.css")

block content  
    h1 Search Results
    .displaying-results
      if noresults
      else
        span= "Results for " + searchValue

    .search-results   
      div
        each article, i in articles
          if i%3==0
            // TODO: 
              // add ads
          .search-result
            img.result-image(src=article.image1)
            .summary
              .result-link
                h3: a.article-link(target="_blank" rel="nofollow noreferrer" href="/article/"+article._id, title=article.title)= "A: "+article.title
              .result-discription= articlelimit
              if article.tag
                .result-tag
                  a.article-tag(href=article.category) abcde
              .result-posted
                b.posted posted &nbsp 
                span(title=article.date)= article.date
                |  by 
                span= article.author
              .clear



    if noresults
      .noresults
        .noresults-value.
          We couldn't find anything for <b>#{searchValue}</b>
        .noresults-hint.
          Try different or less specific keywords.
      .ads    

and I want before rendering the file to the client, I want to substring the article.body

// Search News
app.get('/article/search', function(req, res) {
  if (req.query.search) {
    const regex = new RegExp(escapeRegex(req.query.search), 'gi');

    Article.find({title: regex}, function (err, allArticles) {
      if (err) {
        console.log(err);
      }else if (allArticles === undefined || allArticles.length == 0) {
        console.log(allArticles);
        res.render("article_search", {articles:allArticles, searchValue: req.query.search, noresults:true, totalResults:0});
      }else {
        console.log(allArticles.length);
        res.render("article_search", {articles:allArticles, articlelimit:allArticles.body.substring(0, 5), searchValue: req.query.search, totalResults:thousands_separators(allArticles.length), count:0});
      }

    });
  } else {
    Article.find({}, function (err, allArticles) {
      if (err) {
        console.log(err);
      }else {
        res.render("article_search", {articles:allArticles, articlelimit:allArticles.body.substring(0, 5)});
      }
    });
  }
});

but i tried this ind its gave me that error TypeError: Cannot read property 'substring' of undefined

and now I don't know what to do

I'm using node-js, MongoDB, mongoose and pug

NOTE: I need it ONLY in nodejs because if the user turns off the javascript on pug its display the whole body of the article

Ad

Answer

So after many attempts I get this answer working:

let maximumArticleLenghtDisplay = 100;

for (var i = 0; i < allArticles.length; i++) {
   allArticles[i].body = allArticles[i].body.substring(0, maximumArticleLenghtDisplay);
}

The for loop you need to put after you search the articles

Article.find({title: regex}, function (err, allArticles) {
      for (var i = 0; i < allArticles.length; i++) {
        allArticles[i].body = allArticles[i].body.substring(0, maximumArticleLenghtDisplay);
      }

And then you doesn't need the articlelimit

Ad
source: stackoverflow.com
Ad