Wednesday 3 May 2017

MongoDB: I can't seem to query matches with findOne (using mongoose,mLab)

  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 using koa.js for this. Even though there are matches it cant seem to match.

I connect to mLab with mongoose.connect

const url = require('./models/url'); //require model

This is my schema

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;

the full code is here.



via furball514

No comments:

Post a Comment