简体   繁体   English

在预中间件中未定义的猫鼬模型

[英]Mongoose Model Undefined in Pre Middleware

I am trying to construct an application that acts as a URL shortener like bitly. 我正在尝试构建一个像bitbit一样充当URL缩短器的应用程序。 I have hit a snag, however, and would very much appreciate your help. 但是,我遇到了麻烦,非常感谢您的帮助。 I was in the process of testing my Link model with mocha and was running into errors in one of my pre middleware functions. 我当时正在用Mocha测试我的Link模型,并且在我的中间件前功能之一中遇到错误。 In this function I attempt to get a count of all the entries that have a shortened url that matches the one that I just generated, so that I don't double up on my shortened links. 在此函数中,我尝试获取所有具有与我刚生成的URL匹配的缩短URL的条目的计数,以使我不会在缩短的链接上加倍。 In order to do this I am trying to call the Mongoose's count function on my model, but I am getting the TypeError: "cannot read property 'count' of undefined". 为了做到这一点,我试图在模型上调用Mongoose的count函数,但是我得到了TypeError:“无法读取未定义的属性'count'”。 I have tried to search for why this is happening but have been unable to come up with anything. 我试图搜索为什么会发生这种情况,但无法提出任何建议。

If you could help me figure out why this is happening or what a better method would be for generating the shortened links, I would very much appreciate it. 如果您可以帮助我弄清楚为什么会这样,或者生成缩短链接的更好方法是什么,我将不胜感激。 Thanks! 谢谢!

You can find my code for my Link model below: 您可以在下面找到我的Link模型的代码:

'use strict';

let mongoose = require('mongoose'),
    config = require('../../config/config'),
    schema = mongoose.Schema;

let LinkSchema = new schema({
    originalLink: {
        type: String,
        trim: true,
        validate: [
            function(link) {
                let urlReg = new RegExp("(http|ftp|https)://[\w-]+" +
                    "(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");
                return urlReg.test(link);
            }, 'The URL entered is not valid'
        ],
        required: 'URL to shorten is required'
    },
    shortenedLink: String
});

LinkSchema.pre('save', function(next) {
    let shortLink;
    let count;
    while (true) {
        console.log(`slink: ${shortLink}, count: ${count}`);
        shortLink = this.generateShortenedLink(this.originalLink);
        mongoose.model['Link'].count({shortenedLink : shortLink}, (err, n) => {
            if (err) {
                console.log(err);
                next();
            }
            count = n;
        });
        if (count === 0) {
            break;
        }
    }

    this.shortenedLink = shortLink;

    next();
});

LinkSchema.methods.generateShortenedLink = function(link) {
    let text = "";
    let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (let i = 0; i < 8; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return config.appUrl + text;
};

mongoose.model('Link', LinkSchema);

mongoose.model is a function, and you're using it as if it were an object. mongoose.model是一个函数,您就像在使用它一样是一个对象。

Try this instead: 尝试以下方法:

mongoose.model('Link').count(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM