0
I have a code that sends and receives messages. I can read a local JSON file
With this code in ngOnInit I can read what is inside the JSON file and turn it into a message that is displayed on the screen:
ngOnInit() {
this.emissor = 'Luis Henrique';
this._http.get<PreparacaoDeMensagem[]>("../assets/db/mensagem.json")
.subscribe(mensagem => {
this.mensagem = mensagem
for (let i=0;i < mensagem.length;i++){
this.adicionarMensagem(this.mensagem[i]);
}
})
Now my problem is: add new data to the json file, it’s like this:
[
{
"texto": "Oi galera, como vocês estão?",
"data": "2018-09-25T21:08:52",
"contato": "José"
},
{
"texto": "Estou bem, José, e você?",
"data": "2018-09-25T21:08:52",
"contato": "Maria"
},
{
"texto": "Opa pessoal, tudo bem com vcs?",
"data": "2018-09-25T21:08:52",
"contato": "Luis Henrique"
},
{
"texto": "Opa pessoal, tudo bem com vcs?",
"data": "2018-09-25T21:08:52",
"contato": "Luis Henrique"
},
{
"texto": "Opa pessoal, tudo bem com vcs?",
"data": "2018-09-25T21:08:52",
"contato": "Luis Henrique"
}
I can already transform the message into a format that will be understood by JSON using the JSON.stringify() function, I just need to know how to 'push' the json file.
Follow the code that sends the message and turns it into a json string:
enviar() {
let mensagem = {
texto: this.textoEmEdicao,
data: new Date(),
contato: this.emissor
}
this.adicionarMensagem(mensagem);
this.textoEmEdicao = '';
console.log(JSON.stringify(mensagem)) //Transforma em string json e mostra no console
}
that push would save?
– Willian
No, it would be something like the push function in arrays in javascript, it pushes the other houses of the array and adds a new
– LuisHF
do you want to save in the first position? in the last or in a certain location?
– Willian
I want to save below the existing ones in the code
– LuisHF
luis is a post on file json n resolv?
– Willian
axo que você via ter que passa em um backend
– Willian