1

I am using adonis framework for server. I am storing an array of object in a file and using it in my code as datasource but when I am running a manipulation on imported variable of this array of object, it changes the value of that object for that particular server instance. why is this happening?

below is the code :

import { Ant } from "App/Utils/Ant";

export default class antcontroller {


public async antTocow(ants, color) {
    ants.forEach((ant) => {
      ant["color"] = color;
         });

    return ants;
  }

public async index(ctx: HttpContextContract) {

    let ants = Ant[country];
antToCow = await this.antTocow(
        ants.slice(0, 3),
        color
      );
return antToCow
    }
}
}

cowController

import { Ant } from "App/Utils/Ant";

export default class antcontroller {



public async index(ctx: HttpContextContract) {

    let ants = Ant[country];

return ants
    }
}
}

Ant.js

const ant = {
NEPAL: {
language: "nepaleese",
size:"big"
}}
module.exports = { ants };

on running cow api get call controller I get

{
language: "nepaleese",
size:"big"
}

then on running ant controller I get

{language: "nepaleese",
size:"big",
color:"black"
} 

then now on running cow controller i get:

{language: "nepaleese",
size:"big",
color:"black"
} 

color field should not be in cows call, why is it comming. does my file gets changed in memory for a given instance of server?

0 Answers0