Node.js +猫鼬-每次都不获取数据 [英] Node.js + Mongoose - Not getting data every time

查看:71
本文介绍了Node.js +猫鼬-每次都不获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个下拉列表,它从我从猫鼬获得的一些数据中获取了其链接.

I made a drop-down which gets its links from some data I get with mongoose.

但是它不是持久的.使用完全相同的代码,我不一定总能获得链接的数据.

However its not persistent. With the exact same code, I don't always get my data for the links.

(实际上我的所有东西都是这样,但是我的下拉菜单很简单)

(It's like this for all my things actually, but my drop-downs are simple)

我的下拉菜单(由EJS和引导程序制成)

My drop-down (made with EJS and bootstrap)

<div class="dropdown-menu" aria-labelledby="navdrop">
                        <% schools.forEach((school) => { %>
                            <a href="/skoler/<%= school.adress.city %>" class="dropdown-item"><%= school.name %></a>
                        <% }); %>
                    </div>

(对不起,上面的格式很烂,编辑器不会让我做的更好).

(Sorry for the shitty format above, the editor wouldnt let me make it better).

这是我处理索引页面的途径.

This is my route for handling my index page.

server.get('/', 
    async function(req, res) {
        let schools = await schoolService.getAll();

        res.render('public assets/pages/index', {
            page_title: "Langaming.dk - Index",
            schools: schools
        });
    }
);

这是我的schoolService.getAll();

This is my schoolService.getAll();

"getAll": () => {
    return new Promise(function(resolve, reject){
        School.find({}, function (err, schools) {
            if (err)
                return reject(err)
            else 
                return resolve(schools)
        });
    })
}

我会尽力解释一下.当我进入页面时,有时会显示链接,而有时却不显示. (大多数情况下他们不这样做).一直都是相同的代码.

I will try and explain it a bit better. When I go onto my page, sometimes the links show up, and other times they don't. (Mostly they don't). It's the same code all the time.

该项目正在快速运行以进行路线处理.

The project is running express for route handling.

为什么会这样?

推荐答案

可能由于某种原因,您的数据库没有文档(您是否在请求之间清除数据?),并且不会因为以下原因而引发错误:

Might be that for some reason your database doesn't have documents (are you wiping out data between requests?) and it's not going to throw an error just because of it.

"getAll": () => {
return new Promise(function(resolve, reject){
    School.find({}, function (err, schools) {

        if (err)
            return reject(err)
        if (!schools) { 
            console.log('there are no documents');
            return reject();
        }
        else 
            resolve(schools)
    });
})

}

这篇关于Node.js +猫鼬-每次都不获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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