node.js에서 간단한 웹 서비스를 만들고 있습니다. 안무가를 사용하여 http 호출을 라우팅합니다. 이 코드는 잘 작동 :이 레코드가 반환되고, 잘 작동 : 당신이 볼 수 있듯이Node.js 매개 변수를 mongodb 요청으로 전달
router.get('/search/*', function(req, res, term){
res.writeHead(200, {'Content Type:':'text/plain'});
db.collection('foo').find({'a':1}).toArray(function(err, items){
console.log(items);
res.write(JSON.stringify(items));
res.end();
});
});
는 찾기 방법은 {1 '은'}을 찾습니다. 하지만 라우터에서 검색어에 검색어를 전달하려면 null 응답이 표시됩니다.
router.get('/search/*', function(req, res, term){
res.writeHead(200, {'Content Type:':'text/plain'});
db.collection('foo').find({'a':term}).toArray(function(err, items){
console.log(items);
res.write(JSON.stringify(items));
res.end();
});
});
아이디어가 있으십니까?
편집 : 아래에 제안 된 바와 같이 용어의 가치를 확인 했으므로 예상 한대로 1이됩니다.
용어의 가치를 확인 했습니까 ?? console.log (용어) – simplyharsh
간단히 말해서 예, 예상대로입니다 – 2bard
오류 값을 확인 했습니까? 쿼리를 실행하는 데 문제가 있으면 err은 null이 아니며'items'는 null이됩니다. 항상 확인하는 것이 좋습니다. – mpobrien