无法读取 null 的属性“拆分”

时间:2021-04-09 16:43:41

标签: javascript

我无法显示所有结果,因为在以下频道上没有图像,因此我收到以下消息

$.get('https://wcf.tourinsoft.com/Syndication/3.0/cdt33/c616ab2a-1083-4ba0-b8e2-f7741e443e46/Objects?$format=json', function(data) {
  //$.get('/json/ecranv2.json', function(data){

  var blogs = data.value;

  $(blogs).each(function() {
    var manifs = this.Listingraisonsociale;
    var ouverturecomp = this.Listinginformationsouverture;
    var commune = this.Listingcommune;
    var ouverture = this.Listingouverture;
    var photos = this.Listingphotos;


    //var datatest= this.Listingphotos;
    let output = '';

    let users = this.Listingphotos.split('$');
    //var testsplit = split($);

    for (var i = 0; i < users.length; i++) {
      console.log(users[i]);
      output += '<img src=' + users[i] + '?width=150&height=150&crop=1>';
    }
    $('.target').append('<p>' + manifs + '</p><span>' + output + '</span>');
  });
});

2 个答案:

答案 0 :(得分:0)

one 索引没有属性值。它为空,因此在尝试对其使用 split 之前,您需要检查它是否为空。在这种情况下,简单的真实检查将起作用。

let users = this.Listingphotos ?  this.Listingphotos.split('$') : [];

到位:

$.get('https://wcf.tourinsoft.com/Syndication/3.0/cdt33/c616ab2a-1083-4ba0-b8e2-f7741e443e46/Objects?$format=json', function(data) {
  //$.get('/json/ecranv2.json', function(data){

  var blogs = data.value;

  $(blogs).each(function() {
    console.log(this);
    var manifs = this.Listingraisonsociale;
    var ouverturecomp = this.Listinginformationsouverture;
    var commune = this.Listingcommune;
    var ouverture = this.Listingouverture;
    var photos = this.Listingphotos;


    //var datatest= this.Listingphotos;
    let output = '';

    let users = this.Listingphotos ?  this.Listingphotos.split('$') : [];
    //var testsplit = split($);

    for (var i = 0; i < users.length; i++) {
      console.log(users[i]);
      output += '<img src=' + users[i] + '?width=150&height=150&crop=1>';
    }
    $('.target').append('<p>' + manifs + '</p><span>' + output + '</span>');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="target"></div>

答案 1 :(得分:0)

split 是字符串对象上的方法,它将在实际字符串的引用上调用。

因此,在您的情况下,未在字符串上调用拆分,并且错误消息明确指出我们正在对空对象使用拆分。所以请确保您正在处理的对象是一个实际的字符串,并相应地处理它。

split mdn