如何在Mongodb Node js中使用EJS在前端显示数组? [英] How to display array in frontend using EJS in Mongodb Node js?

查看:104
本文介绍了如何在Mongodb Node js中使用EJS在前端显示数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个应用,需要在Node Js中使用express js将一系列标签保存到MongoDB中。

I'm currently building an app where I need to save an array of tags into MongoDB using express js in Node Js.

这是我用来将标签作为数组保存到MongoDB中的方法。

Here is the method I used for saving my tags as an array into MongoDB.

app.post('/upload', upload.single('meme'), function (req, res) {
    if (req.file) {

//upload to cloudinary. 
cloudinary.v2.uploader.upload(req.file.path, {use_filename: true, folder: 'test'},
    function(error, result) {
//get the image name
var filePath = req.file.originalname; 

//instantiate the meme schema
var newMeme = new meme(); 

newMeme.imgs = filePath; 

//get the caption form field value 
newMeme.caption = req.body.caption;  

//get the tags value from the form field. 

newMeme.tags = req.body.tags.replace(/\s/g,'').split(',');

//cloudinary url
newMeme.url = result.url

//encrypt the filepath 
newMeme.memeid = hash.unique(filePath); 


newMeme.save(function(err){

 if (err) throw err; 

}); 

res.cookie('filename',newMeme.url);

//return res.send('sucessfully uploaded');

res.redirect('meme/'+newMeme.memeid);

}); 

}else{
//throw error message
return res.send('failed to upload'); 

}

})

我的猫鼬模式如下:

var memeSchema = mongoose.Schema ({

imgs: String,       
id: Number,
memeid: String, 
favorites: Number,
url: String,  
caption: String, 
username: String,
tags: [],  
uploadDate: { type: Date, default: Date.now }
});  

保存后,我的数据库看起来像这样:

After saving, my DB looks something like this:

 "tags": [
        "Suprised",
        "Laughing"
    ], 

现在,当我想显示标签时,我无法正确呈现结果。

Now when I want to display the tags, I'm having problem rendering the results properly.

我想要的结果是用每个结果的链接换行显示它们,而不用逗号显示。

My desired result is to display them with link wrap around each result, not with comma.

例如:< a href = / tags / suprised>惊讶的< / a> < a href = / tags / Laughing>惊讶< / a>

这是我得到的结果:< a href = /标签/惊讶的惊讶,笑< / a>

我希望它们分开。

这是我显示结果的路线:

This is my route for displaying the result:

meme.find().sort(url).exec(function(err, result) { 
    //meme.find().sort({url}, function(err, result){

        if (err) throw err; 
        res.render('pages/index', {

            user: req.user, 
            pathi: result.map(i => i.memeid),
            path: result.map(u => u.url),
            caption: result.map(c => c.caption),
            tags: result.map(t => t.tags),  
            Title: 'Meme Search Engine'

        }); 

    });
})   

这是我的EJS模板,用于显示结果:

Here is my EJS template for displaying the result:

 <% for(var i=0; i<tags.length; i++) { %>


      <a href="tags/<%= tags[i] %>"> <%= tags[i] %> </a> 


<%}%> 

我希望不要遗漏任何东西。

I hope am not missing anything.

如何使结果分开而不是用逗号显示?

How do I make my result separate instead of displaying with the comma?

谢谢!

推荐答案

我在Quora中找到了您的问题。好吧,我将在此字段中修改memeSchema:

I found your question in Quora. Well, I would modify the memeSchema in this field:

tags: [String] 

让我知道是否有帮助。如果不是,则将标签[i]分隔为','

Let me know if that helps. If not, split tags[i] by ','

例如:

 <% for(var i=0; i<tags.length; i++) { 
       var tags_array = tags[i];
       tags_array = tags_array.split(',');
       for(var j = 0; j < tags_array.length; j++){%>


      <a href="tags/<%= tags_array[j] %>"> <%=  tags_array[j] %> </a> 

     <%}%> 
<%}%> 

这篇关于如何在Mongodb Node js中使用EJS在前端显示数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆