【问题标题】:Make a filter: Interaction between script and node app制作过滤器:脚本和节点应用程序之间的交互
【发布时间】:2018-05-23 18:43:44
【问题描述】:

我正在尝试为博客制作过滤器功能。我正在使用节点 express/MongoDB/Mongoose 设置,我想要的是单击过滤器以添加“活动”类,并将该过滤器添加到数组 (filterArray) 中。然后,我想将 filterArray 与 Mongoose 的帖子进行比较,并将匹配结果输出到 displayArray,然后循环并输出到页面。

所以我有两组不同的 javascript 数据——我的 Mongoose 对象(存储在我的 MongoDB 数据库中)以及我的脚本文件中的数组和函数。我正在努力研究如何使两者相互配合。

我的问题是:

如果我的数据是从 app.js 传递过来的,我该如何引用 app.js 中的 DOM?

或

如果我必须使用脚本来操作 dom,我如何在那里引用我的 mongoose 数据?

这是我目前所拥有的:

在我的 app.js 中:

app.get('/', function(req, res){
    Post.find({}, function(err, posts){
        if (err) {
            console.log("There was an error: " + err);
        } else {
            res.render('index', {posts: posts});
        }
    });
});

在一个单独的脚本文件中(我在我的 ejs 视图中链接到该文件):

var filterList = ['first item'];
var month = document.getElementsByClassName("post-list__month");

function toggleFilter(element){

    if (element.classList.contains('active')) {
        console.log('active is there so removing it!');
        element.classList.toggle('active');
        filterList = filterList.filter(function(e) { 
            return e !== element.attributes.name.value;
            console.log('Value was removed');
        });
        console.log(filterList);
    } else {
        console.log('active not there so adding it!');
        element.classList.toggle('active');
        filterList.push(element.attributes.name.value);
        console.log(`${element.attributes.name.value} was added to filter list.`);
        console.log(filterList);
    }

}

然后在我看来 index.ejs(我还没有尝试执行过滤...):

<section class="middle-section">

                <% if(posts.length > 0){ %>
                    <% for (var i = posts.length - 1; i > -1; i--) {  %>
                        <a class="post-link" href="/posts/<%= posts[i]._id %>">
                            <div class="post-body">
                                <div class="post">
                                    <div class="post__heading">
                                        <h2 class="heading-secondary"><%= posts[i].title %></h2>
                                    </div>
                                    <div class="post__content">
                                        <p><%= posts[i].body.slice(0, 300) %>...</p>
                                    </div>
                                    <div class="tags">
                                        <span class="heading-secondary-date">
                                            Tags: <%= posts[i].categories[0] %>, <%= posts[i].categories[1] %>
                                        </span>
                                    </div>
                                    <div class="date">
                                        <span class="heading-secondary-date">
                                            <%= posts[i].date %>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </a>
                        <% } %>
                        <% } else { %>
                            <h1>BLOG LIST IS EMPTY<h1>
                                <% } %> 
                            </section>

最后,这是我的 Mongoose Post 架构 - 我将比较类别与 filterList 中的类别:

var postSchema = new mongoose.Schema({
    title: String,
    body: String,
    date: String,
    categories: [],
    month: String,
    author: String,
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }
    ]
});

提前感谢任何可以提供帮助的人:)

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    如果我正确理解了您的问题,那么您想使用过滤器来操作数据。尝试在您的 API 中传递查询参数。

    app.get('/:filter', function(req, res){
        Post.find({}, function(err, posts){
           if (err) {
              console.log("There was an error: " + err);
           } else {
              var filter = req.params.filter;
              // MAKE DB CALL using the
              res.render('index', {posts: posts});
           }
        });
    });
    

    【讨论】:

    • 如果我这样做了,我是否需要让每个过滤器按钮在发布请求中传递?
    • 是的。如果您正在加载,假设第一个实例本身大约有 50 个帖子,然后在前端将其过滤掉并发出异步请求以加载该特定过滤器的更多数据。我对你有意义吗?
    • 是的,我想是的......但这是否意味着我需要使用每个过滤器刷新页面?
    • 没有。您需要为此使用 ajax 并更新您已经呈现的页面。
    • 好的,但这是否也意味着我一次只能添加一个过滤器?我希望能够添加多个过滤器...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2011-02-14
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多