Reputation: 96
I am trying to query OrientDB from an Angular service method. But getting authentication related error. As per my current understanding, I need two GET requests to query OrientDB successfully.
http://localhost:2480/connect/<dbname>
with Authentication headerhttp://localhost:2480/query/<dbname>/sql/<query_text>
But I am getting these errors in browser console:
Below is the code for Angular service
angular.module('ReportApp')
.factory('orientdbService', ['$http', '$log', '$q',
function($http, $log, $q) {
'use strict';
var fac = {};
fac.config = appConfig;
fac.query = function(sql, callback) {
$log.log('Query: '+ sql);
var url=encodeURI(this.config.orientBaseUrl+"query/"+this.config.dbName+"/sql/"+sql);
$log.log('Request URI: '+ url);
connectOrientDb()
.then($http.get(url)
.then(function(response){ //Success
callback(response.data);
},
function(response){ //Failure
callback({
responseStatus: response.status,
responseData: response.data
});
}));
}
fac.fetchCurrentDayReportIdList = function(hostMachineName){
if(typeof(hostMachineName) === "undefined"){
throw {
name: "Parameter Missing",
level: "Show Stopper",
message: "Error detected. This function prameter 'hostMachineName' is mandatory to evaluate return value.",
htmlMessage: "Error detected. This function prameter '<code>hostMachineName</code>' is mandatory to evaluate return value.",
toString: function(){return this.name + ": " + this.message;}
};
}
var sql = "select @rid as rid, runOfDay from TestResult where executorPlatformData.machineName='"+hostMachineName+"'";
var defer = $q.defer()
this.query(sql,
function(data){
defer.resolve(data);
});
return defer.promise;
}
var connectOrientDb = function(){
var url=encodeURI(appConfig.orientBaseUrl+"connect/"+appConfig.dbName);
var req ={
method: 'GET',
url: url,
headers: {
'Authorization': appConfig.authPayload
}
};
$log.log('Request: '+req);
var defer = $q.defer();
$http(req).then(
function(res){ //Success
$log.log('Response: '+res);
defer.resolve(res);
},
function(res){ //Failure
/*throw {
name: "Connection Failed",
level: "Show Stopper",
message: "Error detected ("+response.status+"). Unable to connect to configured database.",
htmlMessage: "Error detected ("+response.status+"). Unable to connect to configured database.",
toString: function(){return this.name + ": " + this.message;}
};*/
defer.reject(res);
}
);
return defer.promise;
}
return fac;
}
]);
I am getting error when fetchCurrentDayReportIdList()
is called.
Can anyone please guide me with a reference implementation or point out what is wrong with my code?
Note:
The global variable
appConfig
holds a JSON object of below format.
{
"orientBaseUrl": "http://localhost:2480/",
"dbName": "atudb",
"dbUser": "root",
"authPayload": "cm9vdDphZG1pbg==",
"dbPassword": "admin",
"formatDateTime": "yyyy-MM-dd HH:mm:ss"
}
Upvotes: 2
Views: 477
Reputation: 1982
in appConfig
try adding Basic
in the authPayLoad
"authPayload": "Basic cm9vdDphZG1pbg=="
and, of course, enable CORS like @wolf4ood said above.
Upvotes: 1