Angular2 / Nativescript:使用RestService时非常奇怪/无用的错误

时间:2016-12-09 09:00:35

标签: angular nativescript

我正忙着开发一款非常基本的条码扫描器应用程序。有两种选择:输入条形码或扫描条形码。当我输入条形码时,我收到以下错误:此错误对我没有任何帮助......任何想法可能是什么原因/我可以看到什么?

我的错误:

JS: EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'getCookie' of null
JS: ORIGINAL STACKTRACE:
JS: Error: Uncaught (in promise): TypeError: Cannot read property 'getCookie' of null
JS:     at resolvePromise (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:416:31)
JS:     at /data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:452:17
JS:     at ZoneDelegate.invokeTask (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:223:37)
JS:     at Object.inner.inner.fork.onInvokeTask (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/core/bundles/core.umd.js:6197:41)
JS:     at ZoneDelegate.invokeTask (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:222:42)
JS:     at Zone.runTask (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:123:47)
JS:     at drainMicroTaskQueue (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:355:35)
JS: Unhandled Promise rejection: Cannot read property 'getCookie' of null ; Zone: angular ; Task: Promise.then ; Value: TypeError: Cannot read property 'getCookie' of null TypeError: Cannot read property 'getCookie' of null
JS:     at CookieXSRFStrategy.configureRequest (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/http/bundles/http.umd.js:1183:91)
JS:     at XHRBackend.createConnection (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/http/bundles/http.umd.js:1223:32)
JS:     at httpRequest (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/http/bundles/http.umd.js:1561:24)
JS:     at Http.request (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/http/bundles/http.umd.js:1661:38)
JS:     at Http.get (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/http/bundles/http.umd.js:1672:25)
JS:     at RestService.getProduct (/data/data/org.nativescript.barcodescanner/files/app/services/rest.service.js:18:26)
JS:     at AppComponent.submitBarcode (/data/data/org.nativescript.barcodescanner/files/app/app.component.js:22:26)
JS:     at /data/data/org.nativescript.barcodescanner/files/app/app.component.js:42:19
JS:     at ZoneDelegate.invoke (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:190:28)
JS:     at Object.inner.inner.fork.onInvoke (/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/core/bundles/core.umd.js:6206:41)
JS: Error: Uncaught (in promise): TypeError: Cannot read property 'getCookie' of null

我的app.module.ts:

import { NgModule, ValueProvider } from "@angular/core";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NativeScriptModule } from "nativescript-angular/platform";

import { HttpModule } from '@angular/http';

import { BarcodeScanner } from "nativescript-barcodescanner";
import { RestService } from './services/rest.service';

import { AppComponent } from "./app.component";






@NgModule({
    imports : [
    NativeScriptModule,     
    NativeScriptFormsModule, 
    HttpModule
    ],
    declarations : [
    AppComponent    
    ],
    providers : [
    RestService,
    BarcodeScanner],
    bootstrap : [AppComponent]      
})
export class AppModule {}

我的app.component.ts:

import { Component, Input, OnInit, Inject } from "@angular/core";
import { BarcodeScanner } from "nativescript-barcodescanner";
import { ProductModel } from './models/product';

import { RestService } from './services/rest.service';



@Component({
    selector: "my-app",
    templateUrl : "./app.component.html"
})
export class AppComponent implements OnInit {
    public barcode: number;
    public textBarcode: number;
    @Input() product: ProductModel;


    public constructor(private restService: RestService, private barcodeScanner: BarcodeScanner) {

    }

    submitTextBarcode() {
        this.restService.getProduct(this.textBarcode)
    .subscribe(
    (res) => {
        this.product = new ProductModel(res.BaseURI, res.CustomError, res.ProviderName, res.RequestFormData, res.RequestURI, res.ResponseCode, res.AvgQty1, res.AvgQty2, res.AvgQty3, res.BarCode, res.Description, res.POSDescription, res.POSPrice, res.ProductCode, res.PurchCount, res.StockOnHand);    },
    (res) => {
        console.log("failure" + res);
    }
    );  
    }


    submitBarcode(barcode: number){ 
    this.restService.getProduct(barcode)
    .subscribe(
    (res) => {
        this.product = new ProductModel(res.BaseURI, res.CustomError, res.ProviderName, res.RequestFormData, res.RequestURI, res.ResponseCode, res.AvgQty1, res.AvgQty2, res.AvgQty3, res.BarCode, res.Description, res.POSDescription, res.POSPrice, res.ProductCode, res.PurchCount, res.StockOnHand);
        //console.log("returned product description: " + this.product.Description);
        //console.log(res);
    },
    (res) => {
        console.log("failure" + res);
    }
    );
    //console.log("product: " + product);

}


    public scan() {
        this.barcodeScanner.scan({
            formats : "EAN_13",
            cancelLabel : "Stop scanning",
            message : "Go scan something Use the volume buttons to turn on the flash",
            preferFrontCamera : false,
            showFlipCameraButton : false
        }).then((result) => {
            this.barcode = +result.text;
            this.submitBarcode(this.barcode);           
        }, (errorMessage) => {
            console.log("Error no scan" + errorMessage);
        });
    }

    public ngOnInit() {     

    }

}

我的app.component.html:

<stackLayout>
<label text="Scan or enter a barcode"></label>
<textField hint="Enter barcode" keyboardType="number" [(ngModel)]="textBarcode"></textField>
<button *ngIf="textBarcode" text="Submit" (tap)="submitTextBarcode()"></button> 
<button text="Scan" (tap)="scan()"></button>

<label *ngIf="product" text="Description"></label>
<label *ngIf="product" [text]="product.Description"></label>

<label *ngIf="product" text="POS Description"></label>
<label *ngIf="product" [text]="product.POSDescription"></label>

<label *ngIf="product" text="POS price"></label>
<label *ngIf="product" [text]="product.POSPrice"></label>

<label *ngIf="product" text="Stock On Hand"></label>
<label *ngIf="product" text="product.StockOnHand"></label>

</stackLayout>

1 个答案:

答案 0 :(得分:1)

基于sample-Groceries tutorial ..请注意此行

import { NativeScriptHttpModule } from "nativescript-angular/http"; 

和导入ni为NativeScriptHttpModule的相同文件..也在同一部分中有以下说明:

  

“... .NativeScriptHttpModule是Angular的NativeScript包装器   HttpModule,一个模块,声明所有Angular的基于HTTP的模块   服务 - 包括UserService使用的Http服务......“