尝试从数据库获取值时,Javascript 'undefined' 错误

标签 javascript asp.net-mvc asp.net-core

我试图从数据库上的数组中获取值,但是每次运行代码时它都会说该值“未定义”。但是当我使用 document.write 时它返回正确的值,为什么呢?以下是返回“未定义”的代码:

 $.get("/api/IgnicoesAPI", function (data) {

        console.log(data);

        $.each(data, function (i, item) {
            //janela de informação de cada marcador


            //console.log(item);
            var infowindow = '<div id="content" style="hight:700px; width:500px">' +
                '<div id="siteNotice">' +
                '</div>' +
                '<div id="bodyContent">' +
                '<p><b>Avaliação da Ocorrência:</b></p>' +
                // iterar todas as ocorrencias para tirar delas as imagens
                item.listaOcorrencias.forEach(function (ocorrencia, o) {
                    ocorrencias.azimute
                });

                +'<p> Latitude: </p>'

            var marker = new L.marker([item.latitude, item.longitude], { icon: ignicao })
                .bindPopup(infowindow)
                .on('click', onClick)
                .addTo(map);




            $('#json map').append(marker);


        }); 
    }); 

我引用的模型 (Ignicoes) 有一个列表属性:

  public class Ignicoes
{

    public Ignicoes()
    {
        ListaOcorrencias = new HashSet<Ocorrencias>();

    }

    public virtual ICollection<Ocorrencias> ListaOcorrencias { get; set; }

}

listaOcorrencias 是我尝试在索引上访问的内容。

enter image description here

当我使用 document.write 时, map 不会出现,但会返回正确的值 enter image description here

最佳答案

 var something;
 item.listaOcorrencias.forEach(function (ocorrencia, o) {
                something = ocorrencias.azimute;
                });
var infowindow = '<div id="content" style="hight:700px; width:500px">' +
                '<div id="siteNotice">' +
                '</div>' +
                '<div id="bodyContent">' +
                '<p><b>Avaliação da Ocorrência:</b></p>' +
                // iterar todas as ocorrencias para tirar delas as imagens
               something

                +'<p> Latitude: </p>'

关于尝试从数据库获取值时,Javascript 'undefined' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60231663/

相关文章:

javascript - 失去焦点时隐藏元素

javascript - 设置 jQuery 元素的属性

javascript - 导入两个 Html.Partial 具有相同 id 的相同 html

javascript - DOM异常: The source image could not be decoded

.net - ASP.NET 保留上传的文件名

c# - MVC5 中的 CodedUITest 在生成代码期间挂起

asp.net-mvc - 如何阻止使用 Entity Framework 在数据库中创建 View 模型

c# - DataProtection - 在多个应用程序之间共享机器 key

ASP.NET Core 将 http 重定向到 https

angular - 在 Angular 6 中处理来自 WebApi 的 400 个错误请求(使用 HttpClient)