Saturday 6 May 2017

insert json into mysql table in node js

I've table users with following field

id,name(varchar) ,gallery(json)

gallery contains the the image path of user uploaded rows, a user can more images to his gallery and the image path of each image is stored in gallery as json

here is a sample row

id name gallery

1  blob ["test.jpg","test1.jpeg"]

Here is my code

function(req,res){
//image path after upload
var imagePath =uploadS3();

//SELECT gallery FROM user   and store to oldGallery
 var oldGallery = getCurrentGallery()
 var updatedGallery;
if(oldGallery==null){
    updatedGallery = "[\""+imagePath+"\"]"

}else{
    //concatinate the gallery row with new imagepath
      updatedGallery = JSON.parse(oldGallery).push(imagePath);
 }

   db.query("UPDATE users SET gallery=? ",[updatedGallery],function(error,result){
});
}

But the problem with JSON.parse(oldGallery).push(imagePath); it didn't worked console.log(JSON.parse(oldGallery).push(imagePath)) outputs 2 instead of the array.



via Jabaa

No comments:

Post a Comment