1
我想我在這裏錯過了一些簡單的東西,但認爲我一直在看這太久。回調響應問題
我原本有你在這裏看到的函數內聯,但想拉出來,並在我的應用程序的其他方面使用它,但我似乎無法得到預期的信息到POST響應。
這裏是我的功能:
let lookUpUserId = (x) => {
mongo.connect(url, (err,db) => {
assert.equal(null,err);
db.collection('data').findOne({"email": {'$eq' : x }},{"_id":1}, (err,result) => {
console.log(result); // I'm getting the correct response here.
assert.equal(null,err);
db.close();
return result; // This seems to be returning empty
});
});
};
而現在這裏的地方,我叫它(簡體):
router.post('/test1', (req,res,next) => {
console.log('Hit on POST /test1');
let obj = '[email protected]';
let a = lookUpUserId(obj);
res.send(a);
});
現在在服務器的控制檯,我得到預期的信息:
{_id:someIdHere }
但是,我得到一個空的身體響應我的POST在客戶端。
任何人都可以在這裏指出正確的方向。
嘗試這兩個解決方案在這裏,第一個完美的作品,雖然我不能讓速記版本正常工作,因爲它是通過「請求未定義」。無論哪種方式,都解決了。 – Gates
@Gates'req was undefined'?我甚至沒有在這裏使用'req'。 – hsz
是的我不太確定,可能是我如何設置路由器todo。我會深入研究它。 – Gates