I have a query I'm passing data from one page to other using local storage. after that i take that data and i make a string . Then i pass that json string to sessionstorage to use as data for my ajax request. but i'm always getting this data as { 'object Object': '' }.
my code ,
const parsedData = JSON.parse(localStorage.getItem('myData'));
console.log(parsedData,"parsedData");
parsedData.data.rows.forEach((result, idx) => {
var a = result.master_id;
var b = result.session_name;
console.log(a,b,"a","b")
var userData = {"pid":a,"session" :b};
console.log( userData ,"userData");
sessionStorage.setItem('user', JSON.stringify(userData));
................. then i access this data in another function,
function graphApi(){
const apiValue = (sessionStorage.getItem('user'));
console.log(apiValue,"apivalue new")
/*var dat ={
"pid":"WEB506",
"session":"WEB506_09092021_M1_S2.csv"
};*/
$.ajax({
type: "POST",
data: apiValue ,
url: "http://localhost:5000/file",
success: function (data) {
console.log(data)
},
error: function(err){
alert(err);
}
Plese help, im stuck with for some time now.
in addition, this is my controller api,
File: async (req, res, next) => {
console.log('---------------');
console.log( req.body); this is where i get { 'object Object': '' }
console.log('---------------');
try{
if(!req.body){
throw new Error("sorry no data, error occured")
}
const {pid, session} = req.body;
const user = await models.graph_data.findAndCountAll({
attributes: [
"document"
],
CodePudding user response:
You're consoling the whole arrayof objects not a single object try
console.log(data.pid) or console.log(data.session)
CodePudding user response:
Try clearing your sesionStorage and try again. The code
{ 'object Object': '' }
is a sign that you used a reference value eg JSON object as a key. However, your code doesn't seem to do that. Try clearing your sessionStorage and try again