Answer the question
In order to leave comments, you need to log in
How to take specific value from mongodb base?
Hello. Here I have certain data in mongodb for example :
{"_id":"56647d86d9742e5c1cce4023","longurl":"http://mongoosejs.com/docs/index.html","shorturl":"KoQrG","__v":0}
app.get('/url/:url', function(req,res){
urls.find({shorturl : req.params.url}, function(err,result){ //urls - это сама база с данными.
if (err) res.json(err);
else res.redirect(result.longurl); //вот тут я хочу перейти на другую страницу
});
});
Loop redirect detected on this page
Answer the question
In order to leave comments, you need to log in
You have a cyclic redirect due to the fact that the base returns an empty array, instead of longurl an empty string and the page starts to be updated cyclically.
And the error is that after returning the data from the database, you need to check that they are there:
app.get('/url/:url', function (req, res){
urls.find({shorturl : req.params.url}, function (err, result){ //urls - это сама база с данными.
if (err) return res.json(err);
if (result.length > 0) {
if ('longurl' in result) {
res.redirect(result.longurl); //вот тут я хочу перейти на другую страницу
} else {
res.json('Longurl is not exists');
}
} else {
res.json('No data');
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question