2016-06-19 7 views
0

데이터베이스 (MongoBD)에 값을 유지하는 콜백 함수가있는 nodeJS 응용 프로그램을 사용하고 있습니다. 아래 콜백 함수 :콜백 함수가 작동하지 않습니다.

exports.add = function(student, cb) { 
    var collection = db.get().collection('students'); 
    collection.insert(student, function(err) { 
     if (err) { 
      throw err; 
     } 
     console.log("Record added"); 
    }); 
} 

상기 콜백 함수는 아래와 같이 다른 fuction를 호출한다 :

router.post("/add", function(req,res){ 
    var student = req.body; 
    Students.add(student, function(err) { 
     if (err) { 
      throw err; 
     } 
     var respOut = JSON.stringify({id:student.id}); 
     console.log("respOut"); 
     res.send(respOut); 
    }); 
}); 
콜백 함수가 상기 굵은 (Students.add) 부로부터 호출

. 현재 DB에 데이터를 유지할 수 있습니다. 콘솔에서 출력을 얻지 못했습니다. (콘솔의 출력은 ->console.log("respOut");) 위의 코드에서 UI의 응답이 있습니다. 콜백 기능에 문제가 있습니까? 아니면 누락 되었습니까?

+0

당신이 exports.add에서 CB (ERR)를 호출하지 않는, 그 이후에해야한다 또는 당신의 console.log ("Record added") 전에 – progysm

답변

0

You ' . 당신의 Students.add 방법에 cb를 사용하지 않는 재

가장 쉬운 그냥 collection.insert를 통해 콜백을 통과하는 것입니다 :

exports.add = function(student, cb) { 
    var collection = db.get().collection('students'); 
    collection.insert(student, cb); 
} 
+0

고마워요. –

관련 문제