How do you get a certain value from the mongodb?nodejs base?
-
Hello, everyone. I've got some data in mongodb:
{ "_id": "56647d86d9742e5c1cce4023", "longurl": "http://mongoosejs.com/docs/index.html", "shorturl": "KoQrG", "__v": 0 }
And there's a ghet's request:
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); //вот тут я хочу перейти на другую страницу }); });
So that's how I'm trying to get there, I'm writing.
Cyclical cross-referencing on this page
Just trying to do that.
res.send(result.longurl)
♪ but it doesn't turn anything away ♪res.send(result)
it removes all the values of this field (_id, longurl,shorturl,_v). But I have to turn to the longurl and take his meaning. How do you do that? Thank you.
-
Reason
What's that?
result
- the mass,result[0]
- The first document found,result[0].longurl
- full web page of the first documentWhere the cycling is from
result.longurl
=undefined
♪ it turns into a change ♪''
, treated as a browser as a relative address, and understood as a " front page " . Brauser's asking for the current page and getting a recount on it. It's a cyclical resection.Which helped to get confused.
You've been misrepresented by design.
res.send(result)
♪result
- a mass of one element,res.send()
leads him to the line.{}
by deleting the square brackets of the mass. If they were usedres.json()
- He'd be bringing him in.[{}]
And you'd find a mistake right away.Decision
User https://ru.stackoverflow.com/questions/472407/%d0%9a%d0%b0%d0%ba-%d0%b2%d0%b7%d1%8f%d1%82%d1%8c-%d0%be%d0%bf%d1%80%d0%b5%d0%b4%d0%b5%d0%bb%d0%b5%d0%bd%d0%bd%d0%be%d0%b5-%d0%b7%d0%bd%d0%b0%d1%87%d0%b5%d0%bd%d0%b8%d0%b5-%d0%b8%d0%b7-%d0%b1%d0%b0%d0%b7%d1%8b-mongodbnodejs#comment531186_472407 You're right. In your place:
res.redirect(result.longurl);// = undefined
You have to do this:
res.redirect(result[0].longurl);// = "http://mongoo..."