我从未在 Node.js 中使用过全局变量,所以我很难理解为什么这不起作用。我声明全局变量是数组,而不是我想将一些对象插入其中,并且为了调试,我只想将其字符串化。 我尝试过这样的:
var test = require('./api/test'); //my class
global.arrayOfObjects = []; //declaring array
global.arrayOfObjects.push = new test(123); //docs3._id is something I return from db
console.log(JSON.stringify(global.arrayOfObjects)); //I get []
最佳答案
关于javascript - 将对象推送到 Node.js 中全局声明的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296971/