简体   繁体   English

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

[英]PhoneGap Barcode Scanner results not being passed to variable

I have a JavaScript function in a PhoneGap app that makes a successful call to the barcode scanner (using the Cordova plugin). 我在PhoneGap应用程序中具有JavaScript函数,该函数成功调用了条形码扫描仪(使用Cordova插件)。 I then form a JSON string and make a 'return' call in an attempt to pass the string back to the function call assignment. 然后,我形成一个JSON字符串并进行“返回”调用,以尝试将该字符串传递回函数调用分配。 I successfully alert the JSON string in the scanning function but then get an undefined value for the variable that's been assigned the function result. 我成功警告了扫描函数中的JSON字符串,但是为分配给函数结果的变量获取了未定义的值。 I'm thinking that this might have to do with scope but declaring the variable outside of the function didn't make any difference. 我认为这可能与范围有关,但是在函数外部声明变量没有任何区别。

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); 
    });

It may be due to the fact you're trying to return myscanJSON within the callback function. 可能是由于您试图在回调函数中返回myscanJSON You could try declaring an empty string outside the callback then append to it like so: 您可以尝试在回调之外声明一个空字符串,然后像这样将其追加:

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;
}

Or you could refactor your callback function: 或者您可以重构您的回调函数:

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; 
}

I refactored the code. 我重构了代码。 It did have a bit of a challenge that I didn't mention. 我确实没有提到它的挑战。 The barcode scanner call needed to be initiated after a successful geolocation call. 成功进行地理定位呼叫后,需要启动条形码扫描器呼叫。 Since these want to complete asynchronously, I had to nest the getScan function inside the geolocation success function, passing in a simple lat/lng JSON object to the scan function which eventually forms a longer JSON string and makes a jQuery AJAX call to write to a database. 由于这些操作想异步完成,因此我不得不将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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM