Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

javascript - TypeError: X.push is not a function - ReadLine to array - NODE JS

I'm trying to create a little json manager but I'm facing a bug, it seems that push is not a valid function. Probably ReadLine returns a string to me (with and without the .toString()) and I can't push in it, I'm looking for good ideas. Thanks

My code:

    module.exports = {
        AddJSON: function (filename = '', obj) {
            const data = this.LoadJSON(filename);
            data.push(obj);
            this.SaveJSON(filename, data);
        },
        LoadJSON: function (filename = '') {
            return JSON.parse(fs.existsSync(filename) ? fs.readFileSync(filename).toString() : 'null');
        },
        SaveJSON: function (filename = '', json) {
            return fs.writeFileSync(filename, JSON.stringify(json))
        }
    };

My Error:

C:...ProjectoolsjsonManager.js:5
            data.push(obj);
                 ^

TypeError: data.push is not a function
    at Object.AddJSON (C:...ProjectoolsjsonManager.js:5:18)
    at Object.<anonymous> (C:...Projectapp.js:13:11)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Process finished with exit code 1

question from:https://stackoverflow.com/questions/66054165/typeerror-x-push-is-not-a-function-readline-to-array-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...