viewModel null inside BroadCastReceive for the first time



Hello Everyone I am facing strange problem with my nativescript app which installed in sonim deveice (
when I installed App for then first time I am getting dockReceive.get(“scanType”) = null in broadcast receive. though I am setting it to load event of home page.
When I clear shared preference or install it for the first time. My viewModel not allow me to set or get it’s
(dockReceive’s) properties in onReceiveCallback(context, intent) function.


       var observableModule = require("data/observable");
 var observableArrayModule = require("data/observable-array");

function DockReceive(info) {
info = info || {};
console.log("in dock receive");
// You can add properties to observables on creation
 var viewModel = new observableModule.fromObject({

uname:info.uname || "",
packingSlip: info.packingSlip || "",
scanType: info.scanType || "",

return viewModel;
module.exports = DockReceive;


     var applicationModule = require("application");
var context = android.content.Context;
var intent = android.content.Intent;
var platform = require("platform");
var dialogsModule = require("ui/dialogs");
var observableArrayModule = require("data/observable-array");
var appSettings = require("application-settings");
var DockReceiveViewModel = require("./shared/view-models/dock-receive-view-model");
var dockReceive = new DockReceiveViewModel();
if ( {
"com.sonim.barcode_read",    ---------------------> (this call when I press yellow hardware button to scan barcode)
function onReceiveCallback(context, intent) {

  if (intent != null) {
    var data = intent.getStringExtra("data"); -------------------> this is working every time.
    console.log("data : : :" + intent.getStringExtra("data"));
    if (data != null && data.length > 0) {
      if (dockReceive !== null) {
        var user = appSettings.getString("uname");
        var type = dockReceive.get("scanType");
 // when I run or install app for the first time the type will be null(blank); so executes default case
// if I restart the app means re run using console then it will work perfectly until i logout from then app.  

        console.log("Scan type is:" + type);

        switch (type) {
          case "ScanPackingSlip":
            dockReceive.set("packingSlip", data);
            dialogsModule.alert("Unknown Scan Type.");

if (appSettings.getBoolean("logedIn") == undefined) {
//Starting point of app
 moduleName: "views/login/login",
 context: dockReceive
} else {
  moduleName: "views/home/home",
context: dockReceive


    <Page xmlns="" loaded="pageLoaded" navigatedFrom="navigateFrom" xmlns:dd="nativescript-drop-down" class="main">
<ActionBar title="Dock Receive" class="menu" id="actionbar">
<NavigationButton text="Go Back" android.systemIcon="ic_menu_back" tap="onNavBtnTap"/>
<ActionItem  text="Delete Batch" id="bdelete" android.position="popup" visibility="hidden" tap="onBatchDelete" />

 <StackLayout class="main">
 <TextField  text="{{ packingSlip }}" hint="Scan Packing Slip" id="pslip"  keyboardType="number" ios.keyboardType="UIKeyboardTypeNumbersAndPunctuation" class="dropdownlist"></TextField>


    exports.pageLoaded = function(args) {
    var page = args.object;
      page.bindingContext = page.navigationContext;
      dockReceive = page.navigationContext;


@multishiv19 hi, could you able to help me with this?


I’m not familiar with this kind of device; however, you seem to be trying to get a value that is not fully initialized and ready yet in the first run, so try to wrap it around some timeout that might help. Also If you have more complicated logic like this that takes some time to initialize, then you better put it in a router page instead of app.js, it will be less error prone


Whenever you use get methods of appSettings, always remember to pass the default parameter as the second argument

appSettings.getString(“user”, “{}”};
appSettings.getBoolean(“loggedIn”, false);

That way you can check if it was initialized or not, easily.
And have a default value if it wasn’t initialized…


thank you everyone.

I am able to find somewhat solution to my problem by checking brocastreceiver in each page where I am scanning barcode using that yellow button, instead of calling it in app.js.