javascript - mongoose 不能用獲取的ajax數據當做查詢條件嗎
問題描述
Ques.find({’author’: ’admin’}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
這樣直接寫能夠獲取到author為admin的數據。
但是換做ajax的數據時, 始終不行
let authors = req.body.author; console.log('服務器收到一個Ajax請求,信息為:', authors); console.log(typeof(authors)) // string let auth = authors console.log(auth) // admin Ques.find({’author’: auth}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
不顯示數據, 說明是沒有找到這個用戶
我又這樣試了試
let auth = ’admin’ Ques.find({’author’: auth}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
這樣也是可以的
ajax請求
let author = XXX; // 動態獲取的 $.ajax({data: {author: author},url: ’/star’,dataType: ’json’,timeout: 2000,type: 'POST',success: function(data){ console.log(data);} });
問題解答
回答1:供參考。因為是AJAX調用過來的,把結果返回到調用的地方顯示,而不是console打印。
Love MongoDB! Have Fun!
相關文章:
1. phpstudy8.1沒集成mysql-front2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker鏡像push報錯4. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題5. node.js - mongodb查找子對象的名稱為某個值的對象的方法6. 前端 - @media query 使用出現的問題?7. javascript - QQ第三方登錄的問題8. html5 - datatables 加載不出來數據。9. javascript - 在 model里定義的 引用表模型時,model為undefined。10. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????

網公網安備