0

So i am creating a javascript searchbar that uses a fetch command to fetch data from a server. This server is not my own and i just used it as a template. This script works fine.But what i want to do is replace the fetch api with my own

Demonstation of code

const userCardTemplate = document.querySelector("[data-user-template]")
    const categoriesSearch = document.querySelector("[data-user-cards-container]")
    const searchInput = document.querySelector("[data-search]")
    
    let users = []

    searchInput.addEventListener("input", e => {
      const value = e.target.value.toLowerCase()
      users.forEach(user => {
        const isVisible = 
        user.name.toLowerCase().includes(value) ||
        user.email.toLowerCase().includes(value)
      user.element.classList.toggle("hide", !isVisible)
      })
    })
    
    fetch("https://jsonplaceholder.typicode.com/users")
    .then(res => res.json())
    .then(data => {
      users = data.map(user => {
        const card = userCardTemplate.content.cloneNode(true).children[0]
        const header = card.querySelector("[data-header]")
        const body = card.querySelector("[data-body]")
        header.textContent = user.name
        body.textContent = user.email
        categoriesSearch.append(card)
        return { name: user.name, email: user.email, element: card}
      })
    })

I think ive made my own fetch api via github which follows.

https://gist.github.com/UllestReal/09ca3d968dda94535e8fc25b998a6ce5#file-gistfile1-txt

But when i replace the first template api with my own, it wont fetch the code. Can someone tell me what im doing wrong?

I just wrote everything in one go it was alot easier. Just look above for my entire problem

2 Answers 2

1

What you have there is just a json file. But whats missing is a server that would serve the file upon a request.

Sign up to request clarification or add additional context in comments.

2 Comments

how do i make that.
Uhmm there isnt an exact answer to that, there are many ways to make a server and also the explanation would be a bit too long so you should probably look it up and decide on your own what to use. What I can suggest is nodejs since it also uses javascript.
0
  1. Go to the link you pasted
  2. Click Raw button

screenshot of raw button

  1. Copy the url

const url = 'https://gist.githubusercontent.com/UllestReal/09ca3d968dda94535e8fc25b998a6ce5/raw/705e7b335cd24e382c15e851ea8888fbdc9cdae4/gistfile1.txt'
fetch(url).then(res => res.json()).then(console.log)

1 Comment

this does work, but occasionally i have to reinput the link due to the values not showing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.