fetch和XMLHttpRequest的问题

时间:2018-05-17 17:10:40

标签: javascript json xmlhttprequest fetch element

首先,我是一个菜鸟,我知道...也许你认为这是一个重复的问题,但我读了很多关于同一个问题的帖子,但没人帮忙。

我正在尝试开发一个关于工作订单,客户端,提供商等的Web应用程序......我有一个Rest API,其中包含连接到数据库(mysql)的正确路由,以及使用CRUM方法的逻辑。我仍然在服务器部分工作,直到几天前一切都很顺利,因为我先用Postman进行了测试,然后进行了一些简单的测试,一切运行良好。

问题是我正在尝试开发我的应用程序的逻辑(真实的)以访问json对象的某些单独元素(API返回一个数组,但这不是问题)。我需要检查并生成具有此格式' number / year'的工作订单的序列号。我尝试使用fetch()和XMLHttpRequest来访问数据,但没有任何作用....我无法访问数组的元素,因为我总是有问题。

如果我在里面尝试这个,如果我的测试使用fetch()它可以工作,但如果我在我的numeroOT()方法中尝试这个我不能,我不知道还有什么要做,所以我需要帮助拜托......我对这件事感到疯狂!!

这是在MY MY TEST中运行的代码:

describe('TEST logica.js', function () {

  it('Genero el Nº de Orden', async () => {

    var numeroDeOT = laLogica.numeroOT(); //this is the method of my logic I'm testing and it's suposed to do the thing


      //This is the the only code which I could acceed to the json. But this has to go inside the numeroOT() method but only works in the test
      //-------------------------------------------

      var response = await fetch('http://localhost:3000/ots');
      var orden = await response.json();
      var laOrden = orden[orden.length-1]; //the last element/json object of the array
      var elNumero = laOrden.numero_ot; //the element I want to check and generate
      console.log(elNumero);
      //The code to check this element and generate the new one goes down here but that's clear



      //---------------------------------------------

    console.log(numeroDeOT);
    expect(numeroDeOT).to.be.a('String'); //this is to check what my method numeroOT() returns. Usually 'undefined'

  }) //it
}); //describe

1 个答案:

答案 0 :(得分:0)

我意识到我在我的代码中尝试的两种方式是不可能将它们用于节点(因为我使用节点),所以我尝试将此代码添加到我的方法中并且它完美地工作我终于可以访问我的JSON对象数组了!

<script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "Event",
          "name": "hello YOGA",
          "startDate": "2018-06-13T19:45",
          "endDate": "2018-06-13T20:45",
          "description":
            "Yoga class at Friends Meeting House (Harrogate) every Thursday.",
          "image": "https://www.hello-yoga.co.uk/yoga-people.svg",
          "location": {
            "@type": "Place",
            "name": "Friends Meeting House",
            "address": {
              "@type": "PostalAddress",
              "streetAddress": "12A Queens Parade",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "postalCode": "HG1 5PP",
              "addressCountry": "GB"
            }
          },
          "offers": {
            "@type": "Offer",
            "availability": "http://schema.org/LimitedAvailability",
            "price": "7.00",
            "priceCurrency": "GBP",
            "validFrom": "2018-05-01T12:00",
            "url": "https://www.hello-yoga.co.uk"
          },
          "potentialAction": {
            "@type": "ExerciseAction",
            "name": "hello YOGA",
            "description": "Small, friendly yoga class. Suitable for all abilities.",
            "exerciseType": "Yoga",
            "startTime": "2018-06-13T19:45",
            "endTime": "2018-06-13T20:45",
            "location": {
              "name": "Friends Meeting House",
              "@type": "PostalAddress",
              "streetAddress": "12A Queens Parade",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "postalCode": "HG1 5PP",
              "addressCountry": "GB"
            }
          },
          "performer": {
            "@type": "LocalBusiness",
            "description":
              "Small, friendly yoga classes. Suitable for all abilities. Strengthen body, increase flexibility and calm the mind.",
            "name": "hello YOGA",
            "email": "clare@hello-yoga.co.uk",
            "url": "https://www.hello-yoga.co.uk",
            "priceRange": "£7",
            "image": "https://www.hello-yoga.co.uk/yoga-people.svg",
            "address": {
              "@type": "PostalAddress",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "addressCountry": "GB"
            }
          }
        }
    </script>
    <script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "Event",
          "name": "hello YOGA",
          "startDate": "2018-06-07T19:00",
          "endDate": "2018-06-07T20:00",
          "description":
            "Yoga class at Beckwith Health Club (Harrogate) every Wednesday.",
          "image": "https://www.hello-yoga.co.uk/yoga-people.svg",
          "location": {
            "@type": "Place",
            "name": "Beckwith Health Club",
            "address": {
              "@type": "PostalAddress",
              "streetAddress": "Beckwith Health Club",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "postalCode": "HG3 1UF",
              "addressCountry": "GB"
            }
          },
          "offers": {
            "@type": "Offer",
            "availability": "http://schema.org/LimitedAvailability",
            "price": "7.00",
            "priceCurrency": "GBP",
            "validFrom": "2018-05-01T12:00",
            "url": "https://www.hello-yoga.co.uk"
          },
          "potentialAction": {
            "@type": "ExerciseAction",
            "name": "hello YOGA",
            "description": "Small, friendly yoga class. Suitable for all abilities.",
            "exerciseType": "Yoga",
            "startTime": "2018-06-07T19:00",
            "endTime": "2018-06-07T20:00",
            "location": {
              "name": "Beckwith Health Club",
              "@type": "PostalAddress",
              "streetAddress": "Beckwith Health Club",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "postalCode": "HG3 1UF",
              "addressCountry": "GB"
            }
          },
          "performer": {
            "@type": "LocalBusiness",
            "description":
              "Small, friendly yoga classes. Suitable for all abilities. Strengthen body, increase flexibility and calm the mind.",
            "name": "hello YOGA",
            "email": "clare@hello-yoga.co.uk",
            "url": "https://www.hello-yoga.co.uk",
            "priceRange": "£7",
            "image": "https://www.hello-yoga.co.uk/yoga-people.svg",
            "address": {
              "@type": "PostalAddress",
              "addressLocality": "Harrogate",
              "addressRegion": "North Yorkshire",
              "addressCountry": "GB"
            }
          }
        }
    </script>