I'm building a website in node js express with ejs view.
I'm taking some data from an array and saving it to session storage.
then I stringify it and catch it on a function call connected to an HTML card click.
but whenever I stringify the parsed data and passed it to the data of ajax.
The API call is not happening giving an undefined error.
Here is my code:
const parsedData = JSON.parse(localStorage.getItem('myData'));
console.log(parsedData, 'parsedData');
var d = JSON.stringify(parsedData);
console.log(d, 'd');
const container = document.getElementById('s1');
parsedData.data.rows.forEach((result, idx) => {
var pid = result.master_id;
var session = result.session_name;
console.log(pid, session, 'a', 'b');
var c = { pid, session };
console.log(c, 'csdc');
var userData = c;
sessionStorage.setItem('user', JSON.stringify(userData));
console.log(userData, 'data for api');
const card = document.createElement('div');
card.classList = 'card';
const content = `
<div class="row">
<div class="card" id="${result.master_id}" onclick="approve(this.id)" >
<div class="card-body" id="${c}" onclick="graphApi(this.id)">
<br>`
...
});
in my ejs. using this id="${c}" onclick i call this function,
function graphApi(id) {
var json = JSON.parse(sessionStorage.getItem('user'));
console.log(json, 'value from card');
var data = JSON.stringify(json);
console.log(data1, 'data1 here');
$.ajax({
type: 'POST',
data: data,
dataType: 'json',
url: 'http://localhost:5000/graphFromcsvfile',
success: function (data) {
alert('success');
console.log(data, 'graph api');
},
error: function (err) {
alert('graph api failed to load');
console.log(err);
},
});
}
I'm getting an ajax error in the browser saying,
responseJSON: {
message: 'Error: WHERE parameter "pid" has invalid "undefined" value';
}
responseText: '{"message":"Error: WHERE parameter \\"pid\\" has invalid \\"undefined\\" value"}';
Regards.