【问题标题】:PhoneGap Barcode Scanner results not being passed to variablePhoneGap 条码扫描仪结果未传递给变量
【发布时间】:2015-03-25 20:28:36
【问题描述】:

我在 PhoneGap 应用程序中有一个 JavaScript 函数,可以成功调用条形码扫描仪(使用 Cordova 插件)。然后我形成一个 JSON 字符串并进行“返回”调用,以尝试将字符串传递回函数调用分配。我成功地警告扫描函数中的 JSON 字符串,但随后为已分配函数结果的变量获得了一个未定义的值。我认为这可能与范围有关,但在函数之外声明变量并没有任何区别。

var myscan = null;
var myclueJSON = null;

var myscan = getScan(); //call scanning function and assign result JSON to myscan variable
alert(myscan); //returns undefined

//call PhoneGap barcode scanner function
//and form JSON to return
function getScan()
{
    var scanner = cordova.require("cordova/plugin/BarcodeScanner");
    scanner.scan( function (result)
    { 
        var myresult = result.text;
        var obj= JSON.parse(myresult);
        //fetch event id from barcode
        var myeventid = obj.eventid;
        //fetch clue sequence from barcode
        var mycluesequence = obj.cluesequence;
        //form JSON string
        var myscanJSON = '{"eventid":"' + myeventid + '","cluesequence":"' + mycluesequence + '"}';
        //return JSON string
        return myscanJSON;
    }, function (error)
    { 
        console.log("Scanning failed: ", error); 
    });

【问题讨论】:

    标签: javascript cordova


    【解决方案1】:

    这可能是因为您试图在回调函数中返回myscanJSON。您可以尝试在回调之外声明一个空字符串,然后像这样附加到它:

    var myscan = null;
    var myclueJSON = null;
    
    var myscan = getScan(); //call scanning function and assign result JSON to myscan variable
    alert(myscan); //returns undefined
    
    //call PhoneGap barcode scanner function
    //and form JSON to return
    function getScan()
    {
        var scanner = cordova.require("cordova/plugin/BarcodeScanner"),
        myscanJSON = '';
        scanner.scan( function (result)
        { 
            var myresult = result.text;
            var obj= JSON.parse(myresult);
            //fetch event id from barcode
            var myeventid = obj.eventid;
            //fetch clue sequence from barcode
            var mycluesequence = obj.cluesequence;
            //form JSON string
            myscanJSON += '{"eventid":"' + myeventid + '","cluesequence":"' + mycluesequence + '"}';
    
        }, function (error)
        { 
            console.log("Scanning failed: ", error); 
        });
    
        //return JSON string
        return myscanJSON;
    }
    

    或者你可以重构你的回调函数:

    var myscan = null;
    var myclueJSON = null;
    
    var myscan = scanner.scan(getScan(result), handleError(error)); //call scanning function and assign result JSON to myscan variable
    alert(myscan); //returns undefined
    
    //call PhoneGap barcode scanner function
    //and form JSON to return
    function getScan(result)
    {
    
      var myresult = result.text;
      var obj= JSON.parse(myresult);
      //fetch event id from barcode
      var myeventid = obj.eventid;
      //fetch clue sequence from barcode
      var mycluesequence = obj.cluesequence;
      //form JSON string
      var myscanJSON = '{"eventid":"' + myeventid + '","cluesequence":"' + mycluesequence + '"}';
      //return JSON string
      return myscanJSON;
    
    }
    
    function handleError(error){
      return error; 
    }
    

    【讨论】:

      【解决方案2】:

      我重构了代码。它确实有一些我没有提到的挑战。成功调用地理定位后,需要启动条形码扫描仪调用。由于这些想要异步完成,我不得不将 getScan 函数嵌套在 geolocation 成功函数中,将一个简单的 lat/lng JSON 对象传递给 scan 函数,最终形成一个更长的 JSON 字符串并进行 jQuery AJAX 调用以写入数据库。

      //get current position function
      function fetchGeolocation()
      {
          navigator.geolocation.getCurrentPosition(fetchCoords, handle_error);
      }
      
      //extract current coords
      function fetchCoords(p)
      {
          mylocjson = '{"lat":"' + p.coords.latitude + '","lng":"' + p.coords.longitude + '"}';
      
      //fire up scanner, passing in lat/lng JSON string
          getScan(mylocjson);
      }
      
      //fire up PG scanner and form JSON result object
      function getScan(incominglocjson)
      {
          //parse lat/lng
          var clueobj = JSON.parse(incominglocjson);
      
          var scanner = cordova.require("cordova/plugin/BarcodeScanner");
          scanner.scan(function (result)
          { 
              var myresult = result.text;
              var obj= JSON.parse(myresult);
              //fetch event id from barcode
              var myclueJSON = '{"eventid":"' + obj.eventid + '","cluesequence":"' + obj.cluesequence + '","lat":"' + clueobj.lat + '","lng":"' + clueobj.lng + '"}';
          }//end scanner.scan()
      
          //make AJAX call to save data
          $.ajax(
          {
              url: "http://myurlhere/myfilename.php",
              type: 'post',
              data:{data:myclueJSON},
              success: function(returndata)
              {
                   //process success returndata
              },
              fail: function(returndata){
                   //process fail returndata
              }
          });//end AJAX call to save data
      }//end getScan()
      
      //handle current position fetch error
      function handle_error(error)
      {
          switch(error.code)
          {
              case error.PERMISSION_DENIED:
                  alert("User denied the request for geolocation.  Please allow access to your GPS system when prompted.");
                  break;
              case error.POSITION_UNAVAILABLE:
                  alert("Location information is unavailable.  Check to see if geolocation is enabled on your device.");
                  break;
              case error.TIMEOUT:
                  alert("The request to get user location timed out.  Try moving to a slightly different location to better access the satellite network.");
                  break;
              case error.UNKNOWN_ERROR:
                  alert("An unknown error occurred.  Call tech support at (999) 999-9999.");
              break;
          }//end error code switch
      }//end handle_error()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        相关资源
        最近更新 更多