Skip to main content
Solved

check if user in specific list


Forum|alt.badge.img+2

Hello 
fetch('https://a.klaviyo.com/api/v2/list/Xa9enW/members?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx&emails=ABC123@gmail.com', {
            mode: "no-cors",
            method: "GET",
            headers: {
                "Accept": "application/json"
            }
    }).then(function (response) {
                return response.json();
            })
            .then(function (data) {
                appendData(data);
            })
            .catch(function (err) {
                console.log('error: ' + err);
            });
        function appendData(data) {
            alert(data[0.id]);  // not working
    var mainContainer = document.getElementById("myData");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                  div.innerHTML = 'Name: ' + data[i].id + ' ' + data[i].created;
                mainContainer.appendChild(div);
            }
        }


Hello I am running this api to get whether this email is in list or  i am getting data inside response.json  but i have created appenddata function in which i want to show this email in page but it is returning  nothing    appendData(data); this is not returning anything  what wrong i am doing i dont know please help 

Also please help me how to pass dynamic email in api for example
​​​​​​https://a.klaviyo.com/api/v2/list/Xa9enW/members?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx&emails={{cutomer.emial}}
when i am passing this it is giving me error 

 

Best answer by jallain

@guptaanjali538 This seems like it could be an issue with what is being passed through your Promise chain. I would recommend adding logging at each step of the way so you can be sure of what data is being passed and what the structure looks like. At least logging the data that you are passing into your appendData function to see if it is in the structure that you think it is.

.then(function (response) {
    return response.json();
})
.then(function (data) {
    // ADD THE LINE BELOW THIS
    console.log(data);
    appendData(data);
})

Or you can add the console.log to the first line of the appendData function itself to see what argument is being passed into it. This should give you an idea of what is going wrong. If they only log `undefined` then you’ll want to try moving the log earlier in the Promise chain. Let me know how that goes.

View original
Did this topic or the replies in the thread help you find an answer to your question?

5 replies

Dov
Forum|alt.badge.img+61
  • Klaviyo Alum
  • 1493 replies
  • June 10, 2021

Hello@guptaanjali538,

Thanks for sharing your question with the Klaviyo Community.

You will need to change alert(data[0.id]) to alert(data[0].id) in your appendData function.

Thanks and have a great day.


Forum|alt.badge.img+2

I have tried this in my append function but it is not working giving me error of 
“error: TypeError: Cannot read property 'id' of undefined”


jallain
Klaviyo Employee
Forum|alt.badge.img+11
  • Klaviyo Employee
  • 70 replies
  • Answer
  • June 11, 2021

@guptaanjali538 This seems like it could be an issue with what is being passed through your Promise chain. I would recommend adding logging at each step of the way so you can be sure of what data is being passed and what the structure looks like. At least logging the data that you are passing into your appendData function to see if it is in the structure that you think it is.

.then(function (response) {
    return response.json();
})
.then(function (data) {
    // ADD THE LINE BELOW THIS
    console.log(data);
    appendData(data);
})

Or you can add the console.log to the first line of the appendData function itself to see what argument is being passed into it. This should give you an idea of what is going wrong. If they only log `undefined` then you’ll want to try moving the log earlier in the Promise chain. Let me know how that goes.


Forum|alt.badge.img+2

Yes I have updated this code but again not working
giving error - error: TypeError: Cannot read property 'emails' of undefined


jallain
Klaviyo Employee
Forum|alt.badge.img+11
  • Klaviyo Employee
  • 70 replies
  • June 11, 2021

@guptaanjali538 that means somewhere in your code you are using `.emails` on something that is undefined.