Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Aurelia获取客户端未从控制器接收数据_Asp.net Mvc_Aurelia_Http Get_Aurelia Fetch Client - Fatal编程技术网

Asp.net mvc Aurelia获取客户端未从控制器接收数据

Asp.net mvc Aurelia获取客户端未从控制器接收数据,asp.net-mvc,aurelia,http-get,aurelia-fetch-client,Asp.net Mvc,Aurelia,Http Get,Aurelia Fetch Client,我试图在控制器(asp.net MVC)中加载数据库,然后使用aurelia fetch客户端将数据从控制器加载到视图中,但aurelia没有提取任何数据(视图表为空,这不是手动声明输入数组时的结果) 员工控制员(控制员) 员工列表(我试图从API获取数据的位置) 在本例中,您可以使用类似的http.get('/api/Employees') 如果您想使用fetch,那么您需要指定一个方法http.fetch('/api/Employees',{method:'GET'})在本例中,您可以使用类

我试图在控制器(asp.net MVC)中加载数据库,然后使用aurelia fetch客户端将数据从控制器加载到视图中,但aurelia没有提取任何数据(视图表为空,这不是手动声明输入数组时的结果)

员工控制员(控制员)

员工列表(我试图从API获取数据的位置)


在本例中,您可以使用类似的
http.get('/api/Employees')


如果您想使用fetch,那么您需要指定一个方法
http.fetch('/api/Employees',{method:'GET'})
在本例中,您可以使用类似的
http.GET('/api/Employees')


如果要使用fetch,则需要指定一个方法
http.fetch('/api/Employees',{method:'GET'})

您看到的问题是因为您没有等待数据返回。创建
employee list
元素时,
EmpAPI
中的
employees
属性仍未定义,因为尚未返回数据获取调用


我看到您有200毫秒的延迟来防止这种情况发生,但有时可能这还不够(我怀疑这一点)。如果你想保持这种策略,也许你可以尝试不同的延迟时间?有不同的方法可以做到这一点,比如只解决
getEmployeeList()
仅在数据获取调用已解决时承诺,进一步延迟调用,等待调用等。

您看到的问题是因为您没有等待数据返回。创建
employee list
元素时,
EmpAPI
中的
employees
属性仍未定义,因为尚未返回数据获取调用

我看到您有200毫秒的延迟来防止这种情况发生,但有时可能这还不够(我怀疑这一点)。如果你想保持这种策略,也许你可以尝试不同的延迟时间?有不同的方法可以做到这一点,比如只解析
getEmployeeList()
promise仅当数据获取调用已解析时,进一步延迟调用,等待调用等

using Microsoft.AspNetCore.Mvc;
using SPAproject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SPAproject.Controllers
{
    [Route("api/[controller]")]
    public class EmployeesController : Controller
    {
        private readonly EmployeesDbContext context;

        public EmployeesController(EmployeesDbContext context)
        {
            this.context = context;
        }

        [HttpGet]
        public IEnumerable<Employee> Get()
        {
            return context.Employees.ToList();
        }
    }
}
import { HttpClient } from 'aurelia-fetch-client';
import { inject } from 'aurelia-framework';

let latency = 200;
let id = 0;


@inject(HttpClient)
export class EmpAPI {
    isRequesting = false;

    constructor(http) {
        this.http = http;
        this.http.configure(config =>
                            config.useStandardConfiguration()
                                    .withDefaults({
                                        mode: 'cors'
                                   }
                                 )
                             );
        this.employees = [];

        http.fetch('/api/Employees')
            .then(x => x.json())
            .then(employees => this.employees = employees);


    }

    getEmployeeList() {
        this.isRequesting = true;
        return new Promise(resolve => {
            setTimeout(() => {
                let results = this.employees.map(x => {
                    return {
                        id: x.id,
                        firstName: x.firstName,
                        lastName: x.lastName,
                        email: x.email
                    }
                });
                resolve(results);
                this.isRequesting = false;
            }, latency);
        });
    }

    getEmployeeDetails(id) {
        this.isRequesting = true;
        return new Promise(resolve => {
            setTimeout(() => {
                let found = this.employees.filter(x => x.id == id)[0];
                resolve(JSON.parse(JSON.stringify(found)));
                this.isRequesting = false;
            }, latency);
        });
    }

    saveEmployee(employee) {
        this.isRequesting = true;
        return new Promise(resolve => {
            setTimeout(() => {
                let instance = JSON.parse(JSON.stringify(employee));
                let found = this.employees.filter(x => x.id == employee.id)[0];

                if (found) {
                    let index = this.employees.indexOf(found);
                    this.employees[index] = instance;
                } else {
                    instance.id = getId();
                    this.employees.push(instance);
                }

                this.isRequesting = false;
                resolve(instance);
            }, latency);
        });
    }
}
import { EventAggregator } from 'aurelia-event-aggregator';
import { EmpAPI } from 'emp-api';
import { inject } from 'aurelia-framework';
import { EmployeeUpdated } from 'employeeUpdated';
import { EmployeeViewed } from 'employeeViewed';

@inject(EmpAPI, EventAggregator)
export class EmployeeList {
    constructor(api, ea) {
        this.api = api;
        this.ea = ea;
        this.employees = [];

        ea.subscribe(EmployeeViewed, msg => this.select(msg.employee));
        ea.subscribe(EmployeeUpdated, msg => {
            let id = msg.employee.id;
            let found = this.employees.find(x => x.id == id);
            Object.assign(found, msg.employee);
        });
    }

    created() {
        this.api.getEmployeeList().then(employees => this.employees = employees);

    }

    select(employee) {
        this.selectedId = employee.id;
        return true;
    }
}