0

Hi i'm using MeaningCloud's api to get the proper object back once it analyses a string of text or a url for the Natural language processing (NLP). But it doesn't return the proper object.

Right now the code returns a string with the text "[Object object]" on the HTML page. I need it to return the results of the api call which returns the proper JSON object(that I can see in the console) in a proper "key/value" pair format on the HTML page.

Here's my script:

const baseURL = "https://api.meaningcloud.com/sentiment-2.1";
const key = "Your_api_key";

const submitBtn = document.getElementById("submitBtn");
submitBtn.addEventListener("click", (e) => {
    e.preventDefault();
    const url = document.getElementById("url").value;

    if (url !== "") {
        getData(baseURL, url, key)
            .then(function (data) {
                postData("/add", { data: data });
            }).then(function () {
                receiveData()
            }).catch(function (error) {
                console.log(error);
                alert("Invalid input");
            })
    }
})

const getData = async (baseURL, url, key) => {
    const res = await fetch(`${baseURL}?key=${key}&txt=${url}`)
    try {
        const data = await res.json();
        return data;
    }
    catch (error) {
        console.log("error", error);
    }
}

const postData = async (url = "", data = {}) => {
    const response = await fetch(url, {
        method: "POST",
        credentials: "same-origin",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            data: data
        })
    });

    try {
        const newData = await response.json();
        return newData;
    } catch (error) {
        console.log(error);
    }
};

const receiveData = async () => {
    const request = await fetch('/all');
    try {
        // Transform into JSON
        const allData = await request.json()
        console.log(allData)
        // Write updated data to DOM elements
        document.getElementById('result').innerHTML = allData;
    }
    catch (error) {
        console.log("error", error);
        // appropriately handle the error
    }
}

I have another main file that's the server.js file which I run using node server.js that renders the html page properly but the script doesn't render the results on the page properly. You can signup on meaningcloud for a free api key that has a very convenient number of calls you can make.

Rohan Asif
  • 31
  • 1
  • 6
  • Please read [ask]. Be more specific that "doesn't return the proper object". Provide a [mcve] (not your whole application) in the question itself (not at the other end of a link). – Quentin Oct 20 '22 at 17:00
  • ok I'll edit this again – Rohan Asif Oct 20 '22 at 17:01
  • 1
    Duplicate: https://stackoverflow.com/questions/4750225/what-does-object-object-mean – Quentin Oct 20 '22 at 17:09
  • 1
    `// Transform into JSON` — You have that backwards. The `json()` method of a request object parses **from** JSON. – Quentin Oct 20 '22 at 17:10
  • ok I understand that... but I'm new to javascript coming from python so I can't quite grasp.. how to parse JSON so that the webpage renders the key/values like "name: Rohan"... to HTML. Should I add new HTML elements as placeholders for these values and then somehow return them from the script somehow... that's where I'm stuck – Rohan Asif Oct 20 '22 at 17:12
  • "Parse" does not mean "Turn into a string". You have already parsed the JSON. You now have a JavaScript object. See the answers on the duplicate. – Quentin Oct 20 '22 at 17:13
  • Ok so I have been able to extract the information by doing key indexing of allData like this : allData["data"]["data"]. The result of this is a proper javascript object. How can I iterate over this?... I know map function but I'm still a newbie at javascript so don't want to use it to iterate – Rohan Asif Oct 20 '22 at 17:46

0 Answers0