猫鼬查找不返回数组

时间:2019-08-28 12:50:40

标签: node.js express mongoose find

我的问题如下:当我使用.find()方法从数据库(猫鼬)中获取数据时,它会获取数据,但是它没有返回数组,而是返回了用逗号分隔的对象,但是我需要遍历数组来映射它并分别显示每个结果,我想知道为什么它不返回数组以及如何将其转换为数组?这是我的代码

await Zazani.find({zaza: {$regex: req.query.keyword, $options: 'i'}})
  .skip((limit * page) - limit)
  .limit(limit)
  .exec((err,zazans)=>{
        const count = ZazanLength.length; //another fetched data for determining length 
        const pages = (Math.ceil(count / limit) == 0 ? 1 : Math.ceil(count / limit))
        res.render('search', {
            zazans,
            currentPage: page,
            totalPages: pages
        })
  }) 

和架构:

const ZazanSchema = new Schema({
   zaza: Schema.Types.String
})

和结果:

{ _id: 5d6571aa4aca2d06a4bc785d, zaza: 'gela12', __v: 0 },
{ _id: 5d6572124aca2d06a4bc785e, zaza: 'gela12', __v: 0 }
...

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以使用toArray方法:

Zazani.find({zaza: {$regex: req.query.keyword, $options: 'i'}})
 .skip((limit * page) - limit)
 .limit(limit).cursor()
 .exec().toArray(function(err, data) {
     console.log(data);
}

答案 1 :(得分:1)

const newCriteria = {
    id: action.payload.id,
    type: action.payload.type,
};

const newState = state.map(gr => {
    if (gr.id !== action.payload.groupId) {
        return gr;
    }

    return {
        ...gr,
        criteria: [...gr.criteria, newCriteria];
    }
});

答案 2 :(得分:0)

header { padding-bottom: 10px; } .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }函数返回一个诺言,我认为您不必在其中传递回调。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
    <label>Enter tab's name : </label>
    <input type="text" id="entree" minlength="4" maxlength="12" size="12">
    <button onclick="addTab()">VALIDER</button>
</header>

<body>
    <div class="tab">
        <button class="tablinks" onclick="openTab(event, '12septembre')">12septembre</button>
        <button class="tablinks" onclick="openTab(event, '7octobre')">7octobre</button>
    </div>
    <div id="12septembre" class="tabcontent">
        <h3>12 septembre</h3>
    </div>
    <div id="7octobre" class="tabcontent">
        <h3>7 octobre</h3>
    </div>
</body>