Ionic 2 - Promise函数(必须是promof错误的实例)

时间:2017-03-17 02:33:50

标签: c# cordova angular typescript ionic2

之前是否有人遇到过这个错误,可能会为我带来一些启示?我被困了几天:(

Promise Error

我尝试使用我的打字稿文件中的Promise函数调用我的Web API函数。基本上我试图将一个键列表(int或string)传递给我的Web API,我在那里遇到了错误。有谁知道如何解决这个问题 (必须是Promof错误的实例)?

控制器:

import { Injectable } from 'angular2/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

export class WorkOrderService {
    private tasks: any;
    private taskItems: any;
    private woSummary: any;


    static get parameters() {
        return [[Http]];
    }

    public constructor(private http: Http) {
    }

public DownloadWO(ClientApiUrl, workOrders) {

    var headers = new Headers();
    headers.append('Content-Type', 'application/json');

    var workOrderKeys = new Array();

    for (let workOrder of workOrders) {
        if (workOrder.checked) {
            workOrderKeys.push(workOrder.WorkOrderKey);
        }
    }

    //ERROR CANT SOLVE
    return new Promise((resolve, reject) => {
        this.http.post(ClientApiUrl + "workorder/UpdateAndLockWorkOrders", workOrders, { headers: headers })
            .map(res => res.json())
            .subscribe(
            data => {
                resolve(data);
            },
            err => {
                reject(err);
            });
    });
}

}

背后代码.ts

import { Component } from '@angular/core';
import { AlertController, ModalController, NavController, LoadingController } from 'ionic-angular';

import { Global } from '../../app/global';

import { LocationService } from '../../services/locationService';
import { DepartmentService } from '../../services/departmentService';
import { WorkOrderService } from '../../services/workOrderService';

import { HomePage } from '../home/home';
import { ModalPmTaskPage } from './modal-pmTask'

@Component({
    selector: 'page-downloadWO',
    templateUrl: 'downloadWO.html'
})

export class DownloadWOPage {
    private sites: any;
    private selectPLocID: string;
    private selectedSite: number;
    private parentLocations: any;
    private selectedPLocation: number;
    private locations: any;
    private selectedLocation: number;
    private departments: any;
    private selectedDepartment: number;
    private fromDate: Date;
    private toDate: Date;
    private workOrders: any;
    private isLock: number;
    private pmTaskItems: any;

    public constructor(
        private navCtrl: NavController,
        private workOrderService: WorkOrderService,
        private locationService: LocationService,
        private departmentService: DepartmentService,
        private global: Global,
        private alertCtrl: AlertController,
        private modalCtrl: ModalController,
        private loadingCtrl: LoadingController) {   
    }

public download() {
        let loader = this.loadingCtrl.create({
            content: "Please wait..."
        });

        //Download & Lock WorkOrders
        this.workOrderService.DownloadWO(this.global.getClientApiUrl(), this.workOrders).then(data => {
            loader.dismiss();
            let alert = this.alertCtrl.create({
                title: 'Success',
                subTitle: 'Work Orders has downloaded sucessfully',
                buttons: [{
                    text: 'OK',
                    handler: () => {
                        this.navCtrl.push(HomePage);
                    }
                }]
            });
            alert.present();
        }).catch(function (err) {
            loader.dismiss();
            let alert = this.alertCtrl.create({
                title: 'Error',
                subTitle: err,
                buttons: [{
                    text: 'OK'
                }]
            });
            alert.present();
        });
    }
}

HTML:

<ion-content>
...
    <ion-item-divider color="primary">Select Work Order</ion-item-divider>
    <ion-scroll scrollY="true" style="height:50vh;">
        <ion-list>
            <ion-item text-wrap *ngFor="let workOrder of workOrders;">
                <ion-checkbox [(ngModel)]="workOrder.checked" >{{workOrder.checked}}</ion-checkbox>
                <ion-label>
                    <h2>
                        <b>Work Order ID:</b> {{workOrder.WorkOrderID}}
                    </h2>
                    <h2>
                        <b>PM Task ID:</b> {{workOrder.TaskId}}
                    </h2>
                    <h2>
                        <b>Asset ID:</b> {{workOrder.AssetID}}
                    </h2>
                    <button ion-button color="secondary" (click)="viewTaskItemFromServer(workOrder.WorkOrderKey)">View PM Task Items</button>
                </ion-label>
            </ion-item>
        </ion-list>
    </ion-scroll>
    <button ion-button block (click)="download()">Download</button>
</ion-content>

C#Web API:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MaxPro_WebService.Interface.Controllers;
using MaxPro_WebService.Interface.Models;
using System.Net.Http.Headers;
using System.Web.Script.Serialization;
using CMMS.AssetsMaintMgmt_BLLAx;
using System.Transactions;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace MaxPro_Client_WebService.Controllers
{
    [RoutePrefix("api/WorkOrder")]
    public class WorkOrderController : ApiController, IWorkOrderController
    {
        [Route("UpdateAndLockWorkOrders")]
        [HttpPost]
        public async Task<HttpResponseMessage> UpdateAndLockWorkOrders([FromBody]List<string> workOrderKeys)
        {
            string errorMsg, exMsg;
            errorMsg = exMsg = string.Empty;
            DateTime dtNow = DateTime.Now;

            errorMsg = exMsg = string.Empty;

            try
            {

                using (var client = new HttpClient())
                {
                    ////IL: TRY TO PASS IN CLASS
                    //client.BaseAddress = new Uri("http://localhost/MaxPro_WebService/");
                    //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    //var response = await client.PostAsJsonAsync("api/WorkOrder/uploadWorkOrders", workOrders);
                    IEnumerable<WorkOrder> workorders = null;
                    client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerBaseURLAddress"]); //Question how to get URL of web service
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var response = await client.PostAsJsonAsync("api/WorkOrder/GetWorkOrders", workOrderKeys);

                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                        //JSON Object TO Work Order class
                        workorders = response.Content.ReadAsAsync<IEnumerable<WorkOrder>>().Result;
                        if (InsertRecords(workorders, out errorMsg))
                        {
                            //IL: 3/2/2017 working for LockWorkOrder
                            //client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerBaseURLAddress"]); //Question how to get URL of web service
                            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                            response = await client.PostAsJsonAsync("api/WorkOrder/LockWorkOrders", workOrderKeys);

                            if (response.IsSuccessStatusCode)
                            {
                                //LOCKING SUCCESSFUL
                                return Request.CreateResponse(HttpStatusCode.OK);
                            }
                            else
                            {
                                //LOCKING UNSUCCESSFUL
                                return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                                //Something has gone wrong, handle it here
                            }
                        }
                        else
                        {
                            //SAVE WO UNSUCCESSFUL
                            return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                            //Something has gone wrong, handle it here
                        }
                    }
                    else
                    {
                        //Something has gone wrong, handle it here
                        return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                    } 
                }
            }
            catch (TransactionAbortedException ex)
            {
                exMsg = ex.Message;
                return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
            }
            catch (ApplicationException ex)
            {
                exMsg = ex.Message;
                return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
            }
            catch (Exception ex)
            {
                exMsg = ex.Message;
                return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
            }
        }
    }
}

Enviroments:

IDE:VS2015 Cordova CLI:6.5.0

Ionic CLI版本:2.2.1

Ionic App Lib版本:2.2.0

操作系统:Windows 7

节点版本:v7.6.0

Xcode版本:未安装

2 个答案:

答案 0 :(得分:2)

Angular 2的http模块返回Observable而非承诺 您可以使用Observable.toPromise()将其转换为承诺,以在您的承诺链中使用。 尝试:

 return new Promise((resolve, reject) => {
        this.http.post(ClientApiUrl + "workorder/UpdateAndLockWorkOrders", workOrders, { headers: headers })
            .map(res => res.json()).toPromise()//convert to promise
            .then(//call then instead of subscribe to form the promise chain.
            data => {
                resolve(data);
            },
            err => {
                reject(err);
            });
    });

另一种方法是简化而不用新承诺封装:

return this.http.post(ClientApiUrl + "workorder/UpdateAndLockWorkOrders", workOrders, { headers: headers })
                .map(res => res.json()).toPromise()//convert to promise

你的捕获到底会从链中得到错误。

答案 1 :(得分:0)

尝试在承诺后指定类型,如下所示:

返回新的承诺&lt;任何&GT; ((解决,拒绝).....