为什么在静态文件中存储的Adonis对象在其导入值上进行操作会更改?
我正在为服务器使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论