###前提・実現したいこと
mongoDBの$whereのqueryで、javascriptの変数を渡すことができない
###発生している問題・エラーメッセージ
node.js+expressでwebアプリを組んでいます。
POST送信でviewのINPUT要素に入っていた値をreq.body.ほにゃららで受け取って
変数profitに格納して、$whereに書きたいです。
以下のように書いたのですが、
('this.price - this.amount > profit' の部分です)
どうやら変数profitの値が渡っていないようです。
正しい方法についてご教示いただければ嬉しいです。
###該当のソースコード
node.js+express
1 2router.post('/', function(req, res, next) { 3 var limit=req.body.limit // 値は25が入っている; 4 var profit=req.body.profit; // 値は1000が入っている 5 6 AmazonDB.$where('this.price - this.amount > profit').sort({'_id':'asc'}).limit(limit).exec(function(err,docs){ 7 if (err) { 8 console.log(err); 9 } 10 res.render('index', { title: 'title', json: docs}); 11 }); 12}); 13
エラーは、
{ [MongoError: ReferenceError: profit is not defined]
name: 'MongoError',
message: 'ReferenceError: profit is not defined',
'$err': 'ReferenceError: profit is not defined',
code: 16722 }
のような感じでnot definedが出ております。
###試したこと
'this.price - this.amount > profit'
を
'this.price - this.amount > 1000'
と書くと正常にqueryは実行されるため、
記述の仕方に問題があるのかなと考えております。
###補足情報(言語/FW/ツール等のバージョンなど)
node v5.1.0
express 4.13.1
mongo v2.6.11
mongoose 4.5.9
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/12 12:00 編集