试图获得标题资本化

时间:2017-02-09 20:01:23

标签: javascript formatting prototype ejs

这是我从某人那里借来的JavaScript:

String.prototype.toTitleCase = function() {
  var i, j, str, lowers, uppers;
  str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  });

  // Certain minor words should be left lowercase unless 
  // they are the first or last words in the string
  lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
  'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
  for (i = 0, j = lowers.length; i < j; i++)
    str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), 
      function(txt) {
        return txt.toLowerCase();
      });

  // Certain words such as initialisms or acronyms should be left uppercase
  uppers = ['Id', 'Tv'];
  for (i = 0, j = uppers.length; i < j; i++)
    str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), 
      uppers[i].toUpperCase());

  return str;
};

出于某种原因,我无法在我的ejs文件中使用此代码......这是我的代码的一部分。

<h3 class="lead campground-name2 thumbnail text-center"><%= campground.name.toTitleCase(); %></h3>

这是我打电话时得到的错误:

TypeError: /home/ubuntu/workspace/YelpCamp/v12/views/campgrounds/show.ejs:6
   4|         <div class='row'>
   5|             <div class="col-md-3">
>> 6|                 <h3 class="lead campground-name2 thumbnail text-center"><%= campground.name.toTitleCase(); %></h3>
   7|                 <div class="list-group">
   8|                     <a href="#description" class="list-group-item active active-first text-center">Description</a>
   9|                     <a href="#comments" class="list-group-item active text-center">Comments</a>

campground.name.toTitleCase is not a function
   at eval (eval at <anonymous> (/home/ubuntu/workspace/YelpCamp/v12/node_modules/ejs/lib/ejs.js:481:12), <anonymous>:52:40)
   at returnedFn (/home/ubuntu/workspace/YelpCamp/v12/node_modules/ejs/lib/ejs.js:512:17)
   at View.exports.renderFile [as engine] (/home/ubuntu/workspace/YelpCamp/v12/node_modules/ejs/lib/ejs.js:364:31)
   at View.render (/home/ubuntu/workspace/YelpCamp/v12/node_modules/express/lib/view.js:126:8)
   at tryRender (/home/ubuntu/workspace/YelpCamp/v12/node_modules/express/lib/application.js:639:10)
   at EventEmitter.render (/home/ubuntu/workspace/YelpCamp/v12/node_modules/express/lib/application.js:591:3)
   at ServerResponse.render (/home/ubuntu/workspace/YelpCamp/v12/node_modules/express/lib/response.js:960:7)
   at /home/ubuntu/workspace/YelpCamp/v12/routes/campgrounds.js:64:17
   at /home/ubuntu/workspace/YelpCamp/v12/node_modules/mongoose/lib/query.js:2313:18
   at process._tickCallback (node.js:369:9)

我知道它已正确链接,因为我在此文件中已有其他功能已在此页面上工作。我是否错误地调用了此方法?它看起来好像应该像.toUpperCase一样被调用,我已经测试并使用了这段代码......任何帮助都会很棒。提前谢谢大家!

以下是我的EJS文件的全部内容。该文件链接在页脚中,如果需要,我可以发布。代码在第6行和第50行,如果这有帮助的话......

<% include ../partials/header %>
<body class="blue">
    <div class='container'>
        <div class='row'>
            <div class="col-md-3">
                <h3 class="lead campground-name2 thumbnail text-center"><%= campground.name.toTitleCase(); %></h3>
                <div class="list-group">
                    <a href="#description" class="list-group-item active active-first text-center">Description</a>
                    <a href="#comments" class="list-group-item active text-center">Comments</a>
                </div>

                <!--RATINGS-->
                <div class="thumbnail thumbnail-ratings">
                    <form class="center-ratings" action="/campgrounds/<%= campground._id %>/ratings" method="POST">
                        <div class="form-group">
                            <label for="rating">Leave a rating:</label>
                            <fieldset class="starability-basic form-group center-ratings" id="rating">
                              <input type="radio" id="first-rate5" name="rating[rating]" value="5" />
                              <label for="first-rate5" title="Amazing">5 stars</label>
                              <input type="radio" id="first-rate4" name="rating[rating]" value="4" />
                              <label for="first-rate4" title="Very good">4 stars</label>
                              <input type="radio" id="first-rate3" name="rating[rating]" value="3" />
                              <label for="first-rate3" title="Average">3 stars</label>
                              <input type="radio" id="first-rate2" name="rating[rating]" value="2" />
                              <label for="first-rate2" title="Not good">2 stars</label>
                              <input type="radio" id="first-rate1" name="rating[rating]" value="1" />
                              <label for="first-rate1" title="Terrible">1 star</label>
                            </fieldset>
                        </div>
                        <div class="form-group">
                            <button class="btn btn-success">
                                Submit rating!
                            </button>
                        </div>
                    </form>
                    <div class="center-ratings">
                        Current Rating: <%= campground.rating.toFixed(2) %>
                    </div>
                </div>
            </div>

            <!--SHOW Image, price, description, comments-->
            <div class='col-md-9 pull-right'>
                <div class='thumbnail'>
                    <div>
                        <img class="img-responsive" src="<%= campground.image %>"> <!--IMAGE-->
                    </div>
                    <div class='caption-full' id="description">
                        <h3 class="pull-right price">$<%= campground.price.toFixed(2) %>/night</h3> <!--PRICE-->
                        <h3 class="campground-name"><%= campground.name.toTitleCase(); %></h3>
                        <pre><%= campground.description %></pre>
                        <!--shows who submitted campground-->
                        <p><span><em>Submitted <%= moment(campground.createdAt).fromNow() %> by <%= campground.author.username %></em></span></p>

                        <!--EDIT/DELETE CAMPGROUND BUTTONS-->
                        <% if(currentUser && campground.author.id.equals(currentUser._id)){ %>
                            <a class='btn btn-sm btn-warning btn-below' href="/campgrounds/<%= campground._id %>/edit">Edit</a>
                            <form id="delete-form" action='/campgrounds/<%= campground._id %>?_method=DELETE' method='POST'>
                                <button class='btn btn-sm btn-danger btn-below'>Delete</button>
                            </form>
                        <% } %>

                        <!--COMMENTS-->
                        <% if(currentUser && campground.comments.length === 0){ %> <!--if no comments, create button at bottom of description-->
                            <div class="text-right comment-0">
                                <a class="btn btn-sm btn-success" href="/campgrounds/<%= campground._id %>/comments/new">Add a comment</a> 
                            </div>
                            <hr>
                        <% } %>
                    </div>
                </div>
                <% if(campground.comments.length > 0) { %> <!--if no comments, hide gray box-->
                <div class='well' id="comments">    
                    <!--comass="text-right"-->
                    <% campground.comments.forEach(function(comment){ %>
                        <div class="row comment-name">
                            <div class="col-md-12">
                                <div class="comment-username">
                                    <strong><%= comment.author.username %></strong>
                                    <!--Make this accurate. Currently always set to 10 days-->
                                    <span class='pull-right'><em><%= moment(comment.createdAt).fromNow() %></em></span>
                                </div>
                                <pre><%= comment.text %></pre>
                                <% if(currentUser && comment.author.id.equals(currentUser._id)){ %>
                                <a class='btn btn-xs btn-warning' href="/campgrounds/<%= campground._id %>/comments/<%= comment._id %>/edit">Edit</a>
                                <form id="delete-form" action='/campgrounds/<%= campground._id %>/comments/<%= comment._id %>?_method=DELETE' method='POST'>
                                    <button class='btn btn-xs btn-danger'>Delete</button>
                                </form>
                                <% } %>
                            </div>
                        </div>
                        <hr>
                    <% }) %>
                    <% if(currentUser){ %>
                        <div class="text-right">
                            <a class="btn btn-success" href="/campgrounds/<%= campground._id %>/comments/new">Add a comment</a>
                        </div>
                    <% } %>
                </div>
                <% } %>
            </div>

        </div>
    </div>
</body>
<% include ../partials/footer %>

0 个答案:

没有答案