0
async function red(ctx) {
let redurl = "//url here";
url.findOne({ shortenedLink: redurl }, (err, data) => {
//find if short url matches long url in db
if (err) throw err;
if (data) {
//if matches then redirect to long url
ctx.redirect(data.url);
console.log("matched");
} else console.error("--"); //getting this error, it doesn't find any matches even though there are
});
}
Im使用koa.js为此。即使有匹配它似乎不匹配。MongoDB:我似乎无法查询匹配与findOne(使用猫鼬,mLab)
我连接到MLAB与mongoose.connect
const url = require('./models/url'); //require model
这是我的架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const urlSchema = new Schema({
url: String,
shortenedLink: String
},{timestamps: true});
const url = mongoose.model('url',urlSchema);
module.exports = url;
完整的代码是here。
'findOne'返回什么?任何错误或什么? –
如果发现不匹配,则返回该错误。我知道有一个事实,即实际上有匹配。 – furball514
您是否正在查询现有集合?如果是这样,那个集合的名称是什么? Mongoose会用你正在显示的代码查询一个名为'urls'(复数)的集合。 – robertklep