Beginning to hit a wall here.
In line 17 of the code below, I am just trying to find the matching collection. Two lines down from that I am trying to fire off a redirect with the matching url.
I am still just not getting the sequence of commands correct for reading this information from mongo. I get the error “TypeError: Cannot read property ‘find’ of undefined”
(If this was sql I would be writing something like FROM urlcntrctr SELECT “url” WHERE key = “123xyz”)
This is the last piece of the puzzle before I can get this working in Node. What am I doing wrong?
ALSO, how is the redirect triggered correctly?
app.get('/:tag(*)', function(req, res) {
var targetnumber = req.params.tag;
sendforRedirect(targetnumber);
closetheconnection()
})
function sendforRedirect(target){
var mongoose = require('mongoose');
var target = target;
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
var mongodbUri = 'mongodb://<username>:<password>@ds159988.mlab.com:59988/urlcntrctr';
mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
var targetArray = mongodbUri.documents.find({ key: target
})
var curr_url = targetArray[0]["url"];
redirect(curr_url);
});
});
}