"_id":{"$id":"61b5eb36029b48135465e766"},
"name":"push-ups","link":"https://google.com",
"image":"https://google.com",
"gender":["0","1","2"],
"goal":["lw","gw","sf"],
"age":60,
"excersietype":"chest",
"__v":0
這是我的資料存盤在資料庫中的方式,我想根據 3 個條件獲取資料
我收到了來自前臺性別目標和年齡的 3 個查詢,據此我必須檢索資料
const gender = req.query.gender;
const age = req.query.age;
const goal = req.query.goal
const level = req.query.level
if (level==='fb'){
const getdata = new Forbeg.find({gender:{$in:gender}},{age:{$lte:age}},{goal:{$in:goal}});
console.log(getdata)
}
這是查找資料的好方法嗎,因為我收到錯誤
UnhandledPromiseRejectionWarning: MongooseError: `Model.find()` cannot run without a model as `this`. Make sure you are not calling `new Model.find()`
我在獲取時遇到上述錯誤
uj5u.com熱心網友回復:
錯誤是明確的:Make sure you are not calling 'new Model.find()'。使用const getdata = Forbeg.find(...).
但是,您將立即遇到下一個問題,因為 Mongoose 模型回傳 thenables(類似 Promise)。console.log(getdata)將登錄Promise<pending>。您需要通過執行以下操作來解決您的資料庫呼叫
Forbeg.find(...).then( getdata => console.log(getData));
或(更好!):
const getdata = await Forbeg.find(...);
console.log(getdata)
更好的是,添加.lean()以獲得簡單的 JSON 資料而不是 Mongoose 物件的陣列(更快),并.exec()獲得真正的 Promise 而不是 thenable :
const getdata = await Forbeg.find(...).lean().exec();
console.log(getdata)
uj5u.com熱心網友回復:
移除新的運營商
const getData = Forbeg.find({gender:{$in:gender}},{age:{$lte:age}},{goal:{$in:goal}});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380247.html
標籤:javascript 节点.js MongoDB 表达 猫鼬
