Persist json file information in Database

Asked

Viewed 96 times

0

I would like some help with a question related to nodejs.

I am trying to save information I receive from a local json file to the database.

this file brings me several objects, and what I need to do is save object by object example:

let pessoa = [
{nome : 'Fulano1' , sobrenome : 'Beltrano1', idade : 80}
{nome : 'Fulano2' , sobrenome : 'Beltrano2', idade : 90}
{nome : 'Fulano3' , sobrenome : 'Beltrano3', idade : 100}]

I’m performing a loop to save each lap, because I need to infer a date on each object.

I’m trying to save used the:

do{}while(i > contador)

Code I’m trying to use:

setUm = async (req, res, next) => {
fs.readFile('./public/assets/images/data_dragon/pt_br/data/set1-pt_br.json' , 'utf8' , function(err , data ){

    if(err){
        return console.log('erro ao ler o arquivo');
    }

    let jsonData = JSON.parse(data);

    let counter = jsonData.length;

    let i = -1;

    do{
        i = i + 1;
        let dataSave = {
            associatedCards : jsonData[i].assets,
            associatedCardRefs : jsonData[i].associatedCardRefs,
            assets : jsonData[i].assets,
            region : jsonData[i].region,
            egionRef : jsonData[i].regionRef,
            attack : jsonData[i].attack,
            cost : jsonData[i].cost,
            health : jsonData[i].health,
            description : jsonData[i].description,
            descriptionRaw : jsonData[i].descriptionRaw,
            levelupDescription : jsonData[i].levelupDescription,
            levelupDescriptionRaw : jsonData[i].levelupDescriptionRaw,
            flavorText : jsonData[i].flavorText,
            artistName : jsonData[i].artistName,
            name : jsonData[i].name,
            cardCode : jsonData[i].cardCode,
            keywords : jsonData[i].keywords,
            keywordRefs : jsonData[i].keywordRefs ,
            spellSpeed : jsonData[i].spellSpeed,
            spellSpeedRef : jsonData[i].spellSpeedRef,
            rarity : jsonData[i].rarity,
            rarityRef : jsonData[i].rarityRef,
            subtype : jsonData[i].subtype,
            subtypes : jsonData[i].subtypes,
            supertype : jsonData[i].supertype,
            type : jsonData[i].type,
            collectible : jsonData[i].collectible,
            set : jsonData[i].set,
        }

        const infosave = new Infocard(dataSave);

        try{
            infosave.save();
        }catch (e) {
            console.error('ROLOU ALGO DE ERRADO: '+ e.message);
        }

    }while(i < counter);

    res.json(dataSave);

});}

Down with my model:

const Infocard = new mongoose.Schema(
{
    associatedCards : [String],
    associatedCardRefs : [String],
    assets : [String],
    region : String,
    regionRef : String,
    attack : Number,
    cost : Number,
    health : Number,
    description : String,
    descriptionRaw : String,
    levelupDescription : String,
    levelupDescriptionRaw : String,
    flavorText : String,
    artistName : String,
    name : String,
    cardCode : String,
    keywords : [String],
    keywordRefs  : [String],
    spellSpeed : String,
    spellSpeedRef : String,
    rarity : String,
    rarityRef : String,
    subtype : String,
    subtypes : [String],
    supertype : String,
    type : String,
    collectible : Boolean,
    set : String,
},{
    timestamps : {createdAt : 'created_at'}
});

But I’m not succeeding. When I call

res.json(jsonData)

json normally loads into the browser with all the information, but when I try to save to the database the items arrive empty

Ideas are appreciated.

Thank you!

  • Do not use metatags in the title they are system use to inform the state of the question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.