【问题标题】:substring string in nodejs and pugnodejs和pug中的子字符串
【发布时间】:2019-10-11 15:49:01
【问题描述】:

我有一个包含此内容的搜索哈巴狗文件

extends layout.pug

block css
  link(rel="stylesheet", 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(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    

并且我想在将文件呈现给客户端之前,我想对 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)});
      }
    });
  }
});

但我试过这个 ind 它给了我那个错误 TypeError: Cannot read property 'substring' of undefined

现在我不知道该怎么办

我正在使用 node-js、MongoDB、mongoose 和 pug

注意:我只在 nodejs 中需要它,因为如果用户在 pug 上关闭 javascript,它会显示文章的整个正文

【问题讨论】:

  • 但是如果你检查allArticles.length 的长度,你对allArticles.body 有什么期望?错误还说它是未定义的,也许allArticles[0].body 有效
  • 我可以做一些循环来遍历所有结果,而不仅仅是第一个吗? @ManuelSpigolon
  • @Nikasmusicandgaming console.log(allArticles) 在执行子字符串之前并在此处发布输出?

标签: node.js mongodb mongoose substring pug


【解决方案1】:

所以经过多次尝试,我得到了这个答案:

let maximumArticleLenghtDisplay = 100;

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

搜索文章后需要放置的for循环

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

然后你就不需要articlelimit了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-12-02
    • 2018-04-02
    相关资源
    最近更新 更多