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. mysql 查詢身份證號字段值有效的數據2. 視頻文件不能播放,怎么辦?3. node.js - nodejs開發中常用的連接mysql的庫4. python bottle跑起來以后,定時執行的任務為什么每次都重復(多)執行一次?5. mysql - 把一個表中的數據count更新到另一個表里?6. 請教使用PDO連接MSSQL數據庫插入是亂碼問題?7. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處8. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題9. visual-studio - Python OpenCV: 奇怪的自動補全問題10. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?
