0

I am trying to fetch data from CouchDB using a URL. Below is my svelte code

<script>
      import { onMount } from "svelte";
  import { DataTable } from "carbon-components-svelte";
    let id=[];
    let startdate=[];
    let EndDate=[];
     const headers = [
    { key: "name", value: "Name" },
    { key: "port", value: "Port" },
         { key: "protocal", value: "protocal" },
    { key: "rule", value: "Rule" },
  ];
     var rows;
    async function getRandomNumber() {
        console.log(id);
        console.log(startdate);
        console.log(EndDate);
        let Xsum=0;
        let Ysum=0;
        let Zsum=0;
        let non = [];
        const res = await fetch(`http://admin:123456789@127.0.0.1:5984/rtls/_design/GetDeatails/_view/AnchorDetails?group_level=4`);
        const data = await res.json();
        const text=data.rows;
    for (var i = 0; i < 8; i++){
    const obj = text[i];
    const some = (obj.value);
        const pos = some.pos;
        const time = some.resultTime;
        Xsum=Xsum+pos.x;
        Ysum=Ysum+pos.y;
        Zsum=Zsum+pos.z;
        non.push({id:i,"name":pos.x,"protocal":pos.y,"port":pos.z,"rule":some.resultTime});
    }   
        
        return non;
}
 
    async function onClickGetNotes() {
        rows= await getRandomNumber()
                
 }

     let selectedRowIds = [];

  $: console.log("selectedRowIds", selectedRowIds);
</script>
<form on:submit|preventDefault={onClickGetNotes}>
    <input label="Tag" bind:value={id.Tag} placeholder="enter id here"/>
    <input type="date" label="StartDate" bind:value={startdate.StartDate} placeholder="enter start date here"/>
    <input type="date" label="EndDate" bind:value={EndDate.EndDate} placeholder="enter end date here"/>
    <button>Submit</button>
</form>

<DataTable batchSelection bind:selectedRowIds {headers} {rows} />

I am getting an error " Failed to execute 'fetch' on 'Window': Request cannot be constructed from a URL that includes credentials " I have even enables CORS in CouchDB How can i solve this problem? thank you in advance

  • 1
    Does this answer your question? [Request with URL that includes credentials](https://stackoverflow.com/questions/45067331/request-with-url-that-includes-credentials) – Thomas Hennes Mar 03 '21 at 09:53
  • I did this ´var headers = new Headers();headers.append('Authorization', 'Basic ' + btoa(admin + ':' + 123456789)); const res = await fetch(`127.0.0.1:5984/rtls/_design/GetDeatails/_view/AnchorDetails?group_level=4`, {headers: headers}); ´ But i am getting an error saying admin is not defined –  Mar 03 '21 at 10:10
  • Both admin and 123456789 are values, not variables. Try `headers.append('Authorization', 'Basic ' + btoa('admin:123456789'))`. – Thomas Hennes Mar 03 '21 at 10:27
  • stupid me. this is making the whole URL as http://localhost:5000/127.0.0.1:5984/rtls/_design/GetDeatails/_view/AnchorDetails?group_level=4 instead of http:/127.0.0.1:5984/rtls/_design/GetDeatails/_view/AnchorDetails?group_level=4 why is that ? –  Mar 03 '21 at 10:59
  • You need to add the protocol at the beginning, otherwise the URL is understood as relative to your current address: `fetch("http://127.0.0.1:5984/rtls/...")` – Thomas Hennes Mar 03 '21 at 11:09
  • Thank you so much. it works perfecrtly fine now –  Mar 03 '21 at 11:55

0 Answers0