Forge api no response

hey there,
I’m currently developing a Jira Service Management app and , i want to use the knowledge base articles, I’ve already set up the resolver in my backend, which you can check out :slight_smile:

resolver.define('getArticles', async (req) => {
  try {
    const res = await api.asApp().requestJira(
      route`/rest/servicedeskapi/knowledgebase/article?query="{query}"&highlight=true`,
      {
        headers: {
          'Accept': 'application/json',
        },
        queryParams: {
          query: 'test', 
        },
      }
    );
    const data = await res.json();
    console.log(data.values)
    return data.values;
  } catch (error) {
    console.error('Error fetching articles:', error);
    return []; 
  }
});

Next, I’ve implemented the resolver call on the frontend.

import React, { useEffect, useState } from 'react';
import { invoke } from '@forge/bridge';

function App() {
  const [articles, setArticles] = useState(null);
  const [query, setQuery] = useState(''); 

  const fetchArticles = async () => {
    try {
      const fetchedArticles = await invoke('getArticles', { query });
      setArticles(fetchedArticles);
    } catch (error) {
      console.error('Error fetching articles:', error);
    }
  };


  useEffect(() => {
    fetchArticles(); 
    fetchServiceDesks();
  }, []);

  const handleQueryChange = (event) => {
    setQuery(event.target.value);
  };

  return (
    <div>
      <input type="text" value={query} onChange={handleQueryChange} placeholder="Enter your search query" />
      <button onClick={fetchArticles}>Search</button>
      {articles ? (
        <ul>
           
          {articles.map((article) => (
            <li key={article.excerpt}>{article.title}</li>
          ))}
        </ul>
      ) : (
        <p>Loading articles...</p>
      )}
    </div>
  );
}

export default App;

in postman its working

However, despite these steps, I’m not receiving any response, and there are no errors showing up in the browser console. Any insights on what might be causing this issue?"

@MohamedNajah,

There appears to be an error in your router, which would appear either in Forge logs or in the command-line with Forge bridge. As shown in the docs about path construction, the route is a tag function for a JavaScript template literal. To explain that in terms of your code, the query variable must be defined as a variable somewhere preceding the path, and the variable must be surrounded with ${}, not double-quotes.

route`/rest/servicedeskapi/knowledgebase/article?query=${query}&highlight=true`,

You might have some other problems in there too. On the front-end, it looks like you are trying to pass the query in an object without a key. I would skip that for now. Focusing on getting the request/response in the resolver to appear in the logs (not the browser). Maybe just hard-code a query variable before the API request.

1 Like

You’re completely correct. The query works fine when I hardcode it, but how can I make it dynamic as i wanted?"