Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm getting this Error

Quote:

[Unhandled promise rejection: TypeError: (0, _fastXmlParser.parse) is not a function. (In '(0, _fastXmlParser.parse)(data)', '(0, _fastXmlParser.parse)' is undefined)]


When using fast-xml-parser package, what I'm trying is to parse this XML and logging it in the console, this is the code so far:-

What I have tried:

import { parse } from 'fast-xml-parser'; function test(){ const emp_number = 20209; fetch('http://website.com/partner/api.php?emp_number='+`${emp_number}`, { method: 'POST', headers: { Accept: 'text/plain', 'Content-Type': 'text/plain' } }) .then((response) => response.text()) .then((data) => { let obj = parse(data); let servicenumber = obj.counter.serviceNumber; let consumpationtype = obj.counter.consumpationtype; let currentread = obj.counter.currentread; let currentdate = obj.counter.currentdate; let lastread = obj.counter.lastread; let lastdate = obj.counter.lastdate; let consumpation = obj.counter.consumpation; console.log("CurrentReading:"+`${currentread}`); });} 

and this is api.php
<?php header('Content-Type: text/plain'); $gecolEmployee = $_GET['emp_number']; $serverName = "host\SqlS"; $connectionInfo = array( "Database"=>"gecol"); $conn = sqlsrv_connect( $serverName, $connectionInfo); $query = "SELECT * FROM readers WHERE emp_number=?"; $result = sqlsrv_query($conn, $query, array($gecolEmployee)); $row = sqlsrv_fetch_array($result); if( $conn ) { echo $row['xml']; }else{ echo "خطأ.<br />"; die( print_r( sqlsrv_errors(), true)); } ?> 

and the xml response
<counter> <serviceNumber>200135644</serviceNumber> <consumpationtype>1</consumpationtype> <currentread>95808</currentread> <currentdate>29/6/2022</currentdate> <lastread>85808</lastread> <lastdate>29/5/2022</lastdate> <consumpation>?</consumpation> <counternumber>7038842</counternumber> <phonenumber>0928065968</phonenumber> </counter>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900