I'm trying to do a single routing back and forth to mongodb, but it seems that on the client side I can't retrieve the data out of the promise received from $resource. On the HTML page there's a button who has ng-click to this following function:
$scope.getToken = function() {
console.log('$scope.getToken()');
authenticationService.getToken($scope.data.email).then(function(result) {
$scope.data.token = result;
});
}
The result received is:
object "m", with fields - 0: "j", 1: "6", 2: "o", 3: "p", 4: "E", 5: "7", 6: "X", 7: "t", $promise: d, $resolved: true, proto: Object
As you can see I receive the token generated, the database seems to work fine and the function invoking as well.
The service on the client side that receive the answer has a function:
function getToken(userEmail) {
var deferred = $q.defer();
$resource(baseUrl + '/getToken').save({
email : userEmail
}, function(result, error) {
if(result) {
deferred.resolve(result);
}
else {
deferred.reject(error);
}
});
return deferred.promise;
}
The function on the server side that sent this:
var router = require('express').Router();
var authenticator = require('../utils/authUtils');
router.post('/getToken', function(request, response) {
authenticator.getToken(
function(error, result) {
if(error) {
//handle error
}
else if(result) {
response.json(result);
}
}
);
And in authUtils.js:
var mongoUtils = require('./mongoUtils');
getToken : function(callback) {
var genToken = randToken.generate(8);
mongoUtils.query(COLLECTIONS.TOKENS, {'token': { $eq: genToken } },
function(error, result) {
if((result && result.length) || error) {
//handle error or duplicates
}
else if(result) {
callback(null, genToken);
}
}
);
}
And in mongoUtils:
query : function(collectionName, query, callback) {
_db.collection(collectionName).find(query).toArray(function (error, result) {
console.log('Utils.query');
if(error) {
//handle error
}
else {
callback(error, result);
}
});
}
Why the server side sends the answer inside an object along with the promise, and how should I handle it?