为什么在静态文件中存储的Adonis对象在其导入值上进行操作会更改?

发布于 2025-01-31 18:27:33 字数 1233 浏览 0 评论 0原文

我正在为服务器使用Adonis框架。我将对象数组存储在文件中,并将其在我的代码中用作数据源,但是当我对该对象数组的导入变量进行操作时,它会更改该对象的值为该特定服务器实例。为什么会发生这种情况?

以下是代码:

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 };

运行牛API获取呼叫控制器的

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

,然后在运行蚂蚁控制器时

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

得到我得到的,然后在运行牛控制器时得到我得到的:

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

颜色字段不应该在牛呼叫中,为什么要开始。对于给定的服务器实例,我的文件是否在内存中更改?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文