In my code, I have a method that queries documentdb for a specific document and returns the results to the caller.
fetch: function(query) {
var fetchDeferred = q.defer();
client.queryDocuments(collectionLink, query).toArray(function(err, docs) {
console.log("Fetch return values - " + JSON.stringify(err) + " - " + JSON.stringify(docs));
if (err) {
fetchDeferred.reject(err);
} else {
fetchDeferred.resolve(docs);
}
});
return fetchDeferred.promise;
}
When I use the fetch method, I noticed the output as follows:
Fetch return values - undefined - []
This indicates that data is being returned from documentDb. In theory, based on the conditions, fetchDeferred should resolve the promise.
The fetch function is then utilized in a GET route handler:
exports.get = function(request, response) {
var userId = '100';
var querySpec = {
query: 'SELECT * FROM root r WHERE r.id = \'user_\' + @id',
parameters: [{
name: '@id',
value: userId
}]
};
docdb.fetch(querySpec).then(function(result){
response.send(statusCodes.OK, {data: result})
}, function(error){
response.send(statusCodes.OK, {data: error});
});
};
Upon calling the route, the expected result should be:
{data:[]}
However, the success function does not seem to be triggered. Any insights into why this might be occurring?