У меня возникли проблемы с моим кодом в node.js
У меня Ajax-вызов в асинхронной функции.Когда я выполняю вызов с опциями, которые я указал перед вызовом в const, тогда программа работает хорошо.
Но когда я вызываю функцию для опций, которая определена в другом скрипте, вызов выполняется вошибка.
Почему это происходит?
// edit //
const request = require('request-promise');
const logger = require('../../../libs/log');
const kasMock = require('../../mock/kasMock')
const remote = require('./remote')
var proxyUrl = "***"
var endstelle = ""
const queryCount = ""
var ticket = ""
const auth = {"api": {username: 'api', password: '***'}, "jira":{username: '***', password: '***}}
var result = {}
var uri = {}
var method = {"put":"PUT", "get":"GET"}
/** Get all tickets */
app.get('/test', function (req, res) {
res.send("Test")
})
app.get('/startDiagnosis', function (req, res) {
//res.send('starte Diagnose')
var param = req.query
ticket = param.ticket
endstelle = param.endstelle
resultKas = pullKAS(endstelle)
uri = {"jiraGetProperties":'***/rest/api/2/issue/' + ticket + '/properties/', "jiraSetProperty": '***/rest/api/2/issue/' + ticket + '/properties/kas-', "kasGetResult":'http://*** + '?_format=json'}
resultKas = pullKAS(endstelle)
let queryCount = jiraGetQueryCount(param.ticket,res)
logger.info("Querycount: " + queryCount)
queryCount.then(function(result){
updateDiagnoseIntern(getResultData(resultKas), param.ticket,result+1)
});
})
async function jiraGetQueryCount(ticket,res) {
//With the commented options the code works fine.
//But when I call the remote.ajaxCallOptions an error occurs.
/*const options = {
method: method.get
,rejectUnauthorized: true
,uri: uri.jiraGetProperties
,auth: auth.jira};
*/
const options = remote.ajaxCallOptions(method.get,uri.jiraGetProperties, auth.jira);
try {
response = await request(options);
res.send(response)
let count = getQueryCount(response)
logger.info('Get Info: ' + count);
return(count)
}
catch (error) {
logger.info("Ein Fehler ist aufgetreten")
return("error")
}
}
В remote.js параметры определены:
module.exports = {
ajaxCallOptionsBody: function(method,uri, auth={}, body={}){
return {
method: method
,rejectUnauthorized: true
,json: true
,uri:uri
,auth: auth
,body: body
}}
,ajaxCallOptions: function(method,uri, auth={}){
return {
method: method
,rejectUnauthorized: true
,json: true
,uri:uri
,auth: auth
}
}};
Выход консоли:
{"message":"Server started on port: 8181","level":"info","timestamp":"2019-02-23T10:40:01.096Z"}
{"message":"HTTP-Request: GET /rest/example/startDiagnosis?ticket=ITSM-45620&endstelle=WELO-9001","level":"info","timestamp":"2019-02-23T10:40:03.441Z"}
{"message":"Querycount: [object Promise]","level":"info","timestamp":"2019-02-23T10:40:03.447Z"}
{"message":"Ein Fehler ist aufgetreten","level":"info","timestamp":"2019-02-23T10:40:03.685Z"}
{"message":"Querycount in Diagnose: error1","level":"info","timestamp":"2019-02-23T10:40:03.687Z"}
{"message":"Update Ticket successfully:","level":"info","timestamp":"2019-02-23T10:40:03.689Z"}
{"message":"HTTP-Response: 200, runtime: 249ms","level":"info","timestamp":"2019-02-23T10:40:03.691Z"}
Есть ли проблема с синхронизацией?
Thx
Феликс