从mongoosecallback函数Node.js获取返回值

我已经写了这样的function

function create(data, res){ var history= new historyData({ owner: data }); historyData.save(function(err, historyData) { //To identify the error if (err) { console.log(err); } //If no error pass the team details to databsae else { return historyData.id; } }); } 

我试图得到这样的返回值

 var history = create("saddsa", res); 

但它给出了undefined错误

我想获得创build的集合的id并将其传递给另一个函数?

尝试返回一个callback并获得该值

 function create(data, res, callback) { var history = new historyData({ owner: data }); historyData.save(function(err, historyData) { //To identify the error if (err) { console.log(err); callback(err) } //If no error pass the team details to databsae else { return callback(null,historyData.id); } }); } 

  // call your function like this create("saddsa", res,function(err,result){ if(!err){ console.log(result) } }); 

更换

return historyData.id;

if(res) res(historyData.id);

并打电话

 create("saddsa", function (id) { var history = id; });