2017-06-01 59 views
1

Unhandled rejection Error: where: "raw query" has been removed, please use where ["raw query", [replacements]]錯誤:其中:「原始查詢」已被刪除,請使用[[原始查詢],[替換]]

我在呈現以下代碼時遇到此錯誤。這是一個動態的where條款,由值產生由用戶在search字段中輸入:

var queryWhere = {id: {$ne: null}}; 
if (req.query) { 
    if (req.query.gender && req.query.gender !== '') { 
     searchGender = "gender = '" + req.query.gender + "'"; 
    } else if (req.query.gender && req.query.gender === '') { 
     searchGender = "gender IS NOT NULL "; 
    } 
    if (req.query.experience && req.query.experience !== '') { 
     searchExperience = "experience = '" + req.query.experience + "'"; 
    } else if (req.query.gender && req.query.experience === '') { 
     searchExperience = "experience IS NOT NULL "; 
    } 
    queryWhere = { 
     $and: [ 
      {$or: [ 
       searchGender, 
       searchExperience]} 
     ] 
    }; 

    models.Users.findAll({ 
     offset: numPerPage * 50, 
     limit: 50, 
     where: queryWhere, 
     include: [ 
      {model: models.Users_Answers} 
     ], 
     order: [ 
      [models.Sequelize.col('id'), 'ASC'], 
      [models.Users_Answers, 'id', 'ASC'] 
     ] 
    }).then(function(answers) { 
     res.render('answers', {answers: answers, search: req.query.searchParam, moment: moment, pagesize: (numPerPage+1), total: totalCount/50}); 
    }); 
} else { 
    res.render('answers'); 
} 

我怎樣才能解決這個問題?

回答

3

原始查詢應該是陣列

queryWhere = { 
    $and: [ 
     {$or: [ 
      [searchGender], 
      [searchExperience]]} 
    ] 
}; 
+0

請如何轉換呢? [Op.or]:{ [Op.like]:'%'+ value +'%', [Op.like]:'%'+ value.toUpperCase()+'%' } – stackdave

1

推薦的方法是使用Sequelize自己的運營商

if (req.query.gender && req.query.gender !== '') { 
    searchGender = {'gender': req.query.gender}; 
} else if (req.query.gender && req.query.gender === '') { 
    searchGender = {'gender': {$not: null}}; 
} 
if (req.query.experience && req.query.experience !== '') { 
    searchExperience = {'experience': req.query.experience}; 
} else if (req.query.gender && req.query.experience === '') { 
    searchExperience = {'experience': {$not: null}}; 
}