0
我有以下:傳遞JSON對象在HTTP請求
路線文件 「prontuarios.js」:
module.exports = function(app){
getProntuarios = function(request, response, next){
var sqlCustom = request.query;
var connection = app.infra.connectionFactory();
var prontuariosDAO = new app.infra.ProntuariosDAO(connection);
prontuariosDAO.lista(sqlCustom, function(erros, resultados){
if(erros){
return next(erros);
}
response.format({
json: function(){
response.json(resultados);
}
});
});
connection.end();
}
app.route('/v1/prontuarios')
.get(getProntuarios);
...
和DAO文件 「ProntuariosDAO.js」
...
ProntuariosDAO.prototype.lista = function(sqlCustom, callback){
var _sqlCustom;
console.log(`sqlCustom ${sqlCustom}`);
if (sqlCustom){
_sqlCustom = sqlCustom;
}
...
當我打電話prontuariosDAO.lista
函數,我希望通過「sqlCustom」參數,但問題是「sqlCustom」必須是函數ProntuariosDAO.prototype.lista
中的JSON對象。但是它沒有發生,sqlCustom參數變得像這樣的字符串:
{
limit: '10',
offset: '2',
orderBy: '{"field":"nome","type":"desc"}',
whereAnd:
[
'{"field":"id","operator":"<","value":"300"}',
'{"field":"nome","operator":"like","value":"jo%"}'
]
}
我已經使用JSON.parse
功能嘗試,但因爲它是解析orderBy
和whereAnd
像一個字符串它沒有正確工作。
這是一種在JSON中轉換該字符串的方法嗎?我是否正確傳遞HTTP參數?