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(mongoose、mLabを使用)との照合ができないようです。
私はmongoose.connect
const url = require('./models/url'); //require model
とMLABに接続これは私のスキーマです:
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