TypeError:Object#<object>没有方法'push'节点js

时间:2016-01-01 19:13:58

标签: node.js

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推送到现有已保存的数组上。

1 个答案:

答案 0 :(得分:0)

从文档中看起来storage.getItemSync返回一个值(可能是也可能不是数组)。因此,我建议添加一个支票:if(!Array.isArray(accounts)){ accounts = []; },然后继续。