when I am trying to add the object to array it while push it in the array I am unable to get push.
console.log('starting password manager');
var storage = require('node-persist');
storage.initSync();
// account.name Facebook
// account.username User12!
// account.password Password123!
accounts = [];
function createAccount (account) {
var accounts = storage.getItemSync('accounts');
if (typeof accounts === 'undefined') {
accounts = [];
}
accounts.push(account);
storage.setItemSync('accounts', accounts);
return account;
}
createAccount({
name: 'Facebook',
username: 'someemail@gmail.com',
password: 'Password123!'
});
我使用npm node-persist来存储var。这将保存我用来保存var并将aobject推送到现有保存的数组上。我使用npm node-persist来存储var。这将保存我用来保存var并将aobject推送到现有已保存的数组上。
答案 0 :(得分:0)
从文档中看起来storage.getItemSync
返回一个值(可能是也可能不是数组)。因此,我建议添加一个支票:if(!Array.isArray(accounts)){ accounts = []; }
,然后继续。