Undefined $scope


#1

Hello I have an app that takes a picture of the camera and stores it in this line:

var url = uploadTask.snapshot.metadata.downloadURLs [0];
$rootScope.myUrl = url;

I have a form that stores the name, email in this line

$scope.formData =
{
name: $ scope.user_username,
email: $ scope.user_email,
}

If I send the form like this at this time, it sends perfectly. But if sending the form by adding the image url like this:

$scope.formData =
{
name: $scope.user_username,
email: $scope.user_email,
myImage: $rootScope.myUrl, // get undefined and not sent form
}

as I can do to save the url in my formData?


#2

Maybe you’re forgeting to inject $rootScope into your controller:

app.controller('MyCtrl', function($scope, $rootScope){
  var url = $rootScope.myUrl; // $rootScope is available
});

Would you show me your code?


#4

UPDATE: WORKS if i do:

var url = uploadTask.snapshot.metadata.downloadURLs [0];
$scope.formData =
{
name: $scope.user_username,
email: $scope.user_email,
myImage: url // WORKS but another fields are CLEAR or loss data
}

but another fields are CLEAR or loss data and repeat code


#5

When you try myImage: $rootScope.myUrl do you get $rootScope is not defined, or $rootScope.myUrl is not defined?


#6

$rootScope.myUrl is undefined only when put this into formData,

It is as if the variable url contains only by seconds the image url and then left empty

var url = uploadTask.snapshot.metadata.downloadURLs [0];

I will prepare all code so that you can see better, I appreciate the help


#7

Acctually, I think you’re trying to access $rootScope.myUrl before it’s been assined.

As you’re setting the value in an asynchronous way on state_changed event, you must be sure the event has been fired and $rootScope.myUrl has been assineg before you can access it.


#8

I think that may be what I will try, thank you very much


#9
uploadTask.on('state_changed', null, function(error) {
   //
}, function() {
   var url = uploadTask.snapshot.metadata.downloadURLs[0];
   $scope.formData.myImage = url  // assing it only when you have it
});

// Note that 'state_changed' hasn't been fired yet when you get here
$scope.formData = {
  usuario: $scope.user_info.displayName,
  email: $scope.user_info.email
};

#11

I just try and if it works !!! I’m happy, I’m just learning and was days without solving it, I appreciate it