'I don't know how to store an array of object with mysql/node correctly
I tried to make a table where i can register list of items. Actually everything is working, except array-object storage.
I tried a LOOOT of things on it, Blob format on my sql- but i can't get the data back in front(nuxt) with .JSON() (.JSON() is not recognized) with this method. Convert Buffer Data
I tried to store it as a JSON format but it's not recognized by mysql.
Now I put it on a TEXT format on my sql (but when i call back the data i only get [object Object]- can't get through it in front or in the back.
I'd like to store something like
[
{"quantity":"2","content":
{"id":21,"reference":"LEG080276","designation":"cadre saillie mozaic blanc 2x6/8 modules 2x3 postes - prof 50mm","description":"sens de montage (horizontal) nombre de modules (12)"}
},
{"quantity":"2","content":
{"id":6,"reference":"LEG080260L","designation":"voyant 2 modules 0.03w pour support batibox 1 poste ","description":null}
}
]
This is the call route in node.js- The list (array of objects) should be stored in content
router.post('/list', function getApi(req, res) {
const {user_id, to_uuid, content, message, list_name, status}= req.body;
db.query(`INSERT INTO list (user_id, to_uuid, content, message, list_name, status) VALUES (?,?,?,?,?,?)`, [user_id, to_uuid, content, message, list_name, status], (err, results) => {
if (err) {
res.status(500).send(`Erreur lors de l'enregistrement de la liste`);
console.log(err);
}
res.status(200).send('Liste Enregistrée');
});
});
is there someone who's have an idea?
Thanks in advance
Solution 1:[1]
So i made it!!!
I registered array of objects (which was stringified) as a text format in mysql workbench!
it worked!!
this.$axios.post('http://localhost:3000/api/list/', {
'user_id': this.$auth.user[0].user_id,
'to_uuid': this.$store.state.list.to_user.user_id,
'content': JSON.stringify(this.$store.state.list.finalList),
'message': this.message,
'list_name': this.name,
'status': "en attente"
})
this part is when i post the stringified array on content
async created(){
const listReceived = await this.$axios.$get(`/api/listReceived/${this.$auth.user[0].user_id}`)
const usersList = await this.$axios.$get("/api/users")
listReceived.forEach(element => {
const userdata=usersList.filter((post)=>post.user_id===element.user_id)
const userSelected={
listId:element.list_id,
listName:element.list_name,
message:element.message,
status:element.status,
content: JSON.parse(element.content),
nickname:userdata[0].nickname
}
this.finalList.push(userSelected)
})
this part is when i get the stringified array - I JSON.parse it on content
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
