# Return of ionic barcode scanner in factory service, undefined

**URL:** <https://forum.ionicframework.com/t/return-of-ionic-barcode-scanner-in-factory-service-undefined/52141>\
**Category:** ionic-v1\
**Created:** [May 16, 2016, 7:53am UTC](https://forum.ionicframework.com/t/return-of-ionic-barcode-scanner-in-factory-service-undefined/52141 "2016-05-16T07:53:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raeon](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@raeon](https://forum.ionicframework.com/u/raeon)\
**Post date:** [May 16, 2016, 7:53am UTC](https://forum.ionicframework.com/t/return-of-ionic-barcode-scanner-in-factory-service-undefined/52141/1 "2016-05-16T07:53:39Z")

</div>

Hi, I’m building with Ionic framework.  
I need to use the QR scan code. After the scanner of QR code, I need to  
select a substring of the return string by the scanner. I want to put  
the QR scan code inside the factory service, but I have the problem with  
the return variable in controller of Angular js. This return a  
undefined variable.  
the factory code I create is this:

> app.factory(“qrCodeService”,[‘$cordovaBarcodeScanner’,‘$ionicPlatform’,  
> function(barcodeScanner , ionicPlatform){  
> var \_id = function(){  
> var p = ionicPlatform.ready(function(){  
> var cod = barcodeScanner.scan()  
> .then(function(imageData){  
> var \_link = imageData.text;  
> var \_pos\_slash = \_link.lastIndexOf(‘/’);  
> return \_link.substr(\_pos\_slash+1);  
> })  
> .catch(function(err){ return “0000000”; });

> ```
> return cod;
> 
> ```

> ```
> }); 
> return p;
> };
> 
> ```

> ```
> return{
> getId : _id
> }     
> 
> ```

> }]);

the controller that i build is this:

> app.controller(“indexCtrl”,[‘$scope’,‘$http’,  
> function ( scope , http ){

> ```
> scope.codeScanner=function(){
> var id = qrCodeService.getId();
> alert(id); /*this return undefined*/
> 
> ```

> ```
> }
> 
> ```

> }

Some of you have a good advice to find a better quality solution?

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [May 16, 2016, 8:27am UTC](https://forum.ionicframework.com/t/return-of-ionic-barcode-scanner-in-factory-service-undefined/52141/2 "2016-05-16T08:27:18Z")

</div>

you are calling async code --\> promises (.then, .catch). So the id is not set, if you call qrCodeService.getId() in the controller.

You should move the whole scanning functionality in a service function you call.

---

<div class="post-metadata">

**Author:** ![raeon](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@raeon](https://forum.ionicframework.com/u/raeon)\
**Post date:** [May 16, 2016, 8:59am UTC](https://forum.ionicframework.com/t/return-of-ionic-barcode-scanner-in-factory-service-undefined/52141/3 "2016-05-16T08:59:08Z")

</div>

Thanks bengtler,  
but I can’t understand how I can return the id parameter to My controller.js (I need the id information in my controller…)…
