PhoneGap条形码扫描器结果未传递给变量

时间:2015-03-25 20:28:37

标签: javascript cordova

我在PhoneGap应用程序中有一个JavaScript函数,可以成功调用条形码扫描程序(使用Cordova插件)。然后我形成一个JSON字符串并进行'return'调用,试图将字符串传递回函数调用赋值。我成功地在扫描函数中警告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); 
    });

2 个答案:

答案 0 :(得分:0)

这可能是因为您试图在回调函数中返回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; 
}

答案 1 :(得分:0)

我重构了代码。它确实有一些我没有提到的挑战。成功进行地理定位呼叫后,需要启动条形码扫描器呼叫。由于这些想要异步完成,我不得不将getScan函数嵌套在地理定位成功函数中,将一个简单的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()