我有一个我正在填充的自定义数据类型,我已经验证数据是否正确填充但是当我尝试将其推送到数组时,结果是数组和自定义数据类型变量都为null。以下是我正在做的一个例子:
{ Error: connect ECONNREFUSED 127.0.0.1:5432
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432 }
// Here is how everything is set up:
const connection = require('./db/knexfile.js').development;
const knex = require('knex')(connection);
const app = express();
const server = app.listen(PORT, '127.0.0.1', 'localhost', () => console.log(`Listening on ${ PORT }`));
// Inside knexfile.js
module.exports = {
development: {
client: 'pg',
connection: {
"user": "development",
"password": "development",
"database": "testdb",
"host": "127.0.0.1",
"port": 5432
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
当我在var values = [];
var temp = {};
temp['one'] = rows[i].cells[1].innerText.trim();
temp['two'] = rows[i].cells[2].innerText.trim();
temp['three'] = rows[i].cells[3].innerText.trim();
temp['four'] = rows[i].cells[4].innerText.trim();
values.push(temp);
之前输出temp时,所有预期值都存在。在values.push(temp);
之后输出values
或temp
时都为空。
非常困惑,感谢任何帮助。
答案 0 :(得分:2)
像这样声明数组和对象..
var values = [];
var temp = {};
示例:
var values = [];
var temp = {};
temp['one'] = 'one';
temp['two'] = 'two';
temp['three'] = 'three';
temp['four'] = 'four';
values.push(temp);
console.log(values);

答案 1 :(得分:0)
这对我来说是一个愚蠢的错误,与.push无关,但是想要回答其他有问题的人。
var values = [];
在任何功能之外宣布,我认为我可以做到这一点,这将是全球性的,但这是一个新手的错误。