0
我有這段代碼:Reviews.find({createdAt : {"$lt" : app.locals.lastDate}})
我想動態地將$lt
更改爲$gt
。。 {[CastError:投射日期在路徑「createdAt」處的值「[object Object]」失敗)
app.post("/scroll", function(req, res){
console.log("req.body...", req.body);
var sortCreate = req.body.older == true ? "createdAt" : "-createdAt"
var greaterOrLess = req.body.older == true ? "$gt" : "$lt";
greaterOrLess = greaterOrLess.toString()
console.log("greaterOrLess;;;", greaterOrLess)
console.log("inside scroll app.locals.lastDate", app.locals.lastDate)
Reviews.find({createdAt : {"$lt" : app.locals.lastDate}})
.limit(10)
.sort(sortCreate).exec()
的問題是,當我改變Reviews.find({createdAt : {"$lt" : app.locals.lastDate}})
到
Reviews.find({createdAt : {greaterOrLess : app.locals.lastDate}})
我得到錯誤:
{ [CastError: Cast to date failed for value "[object Object]" at path "createdAt
"]
message: 'Cast to date failed for value "[object Object]" at path "createdAt"'
,
name: 'CastError',
kind: 'date',
value: { greaterOrLess: Sun Aug 07 2016 19:25:48 GMT-0700 (Pacific Daylight Ti
me) },
path: 'createdAt',
reason: undefined }
這一部分:console.log("greaterOrLess;;;", greaterOrLess)
打印出$gt
或$lt
當我想要那麼爲什麼我不能使用greaterOrLess
變量?它看起來像一個字符串,所以我不知道爲什麼變量操作符不工作。