最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - create a .json file using nodejs - Stack Overflow

programmeradmin0浏览0评论

I'm trying to do a function with JS and nodejs to create a .json file with my MongoDB data in it.

But when I run my function my console show me

Uncaught TypeError: Cannot read property 'prototype' of undefined.

I've done some research and my console that writeJsonFile's value is an object with no value, I don't know if it's the cause of my problem

json(event){ 
 count = Subs.find({}).count();
  var json;
  var obj;
  Subs.find({}).forEach(function (div) {
    var sub_type = div.type;
    var sub_TO = div.text;
    var man = div.topic;
    obj = {[man]: []};
    obj[man].push({"type": sub_type, "TO": sub_TO});
    json = JSON.stringify(obj);
    const writeJsonFile = require('write-json-file');

    writeJsonFile('foo.json', {foo: true}).then(() => {
      console.log('done');
    });
    console.log(json);
  });
}

I'm trying to do a function with JS and nodejs to create a .json file with my MongoDB data in it.

But when I run my function my console show me

Uncaught TypeError: Cannot read property 'prototype' of undefined.

I've done some research and my console that writeJsonFile's value is an object with no value, I don't know if it's the cause of my problem

json(event){ 
 count = Subs.find({}).count();
  var json;
  var obj;
  Subs.find({}).forEach(function (div) {
    var sub_type = div.type;
    var sub_TO = div.text;
    var man = div.topic;
    obj = {[man]: []};
    obj[man].push({"type": sub_type, "TO": sub_TO});
    json = JSON.stringify(obj);
    const writeJsonFile = require('write-json-file');

    writeJsonFile('foo.json', {foo: true}).then(() => {
      console.log('done');
    });
    console.log(json);
  });
}
Share Improve this question edited Nov 27, 2017 at 15:20 alpheonix asked Nov 27, 2017 at 15:05 alpheonixalpheonix 3132 gold badges5 silver badges13 bronze badges 5
  • On what line do you get this error? And do you still get the error if you remove all code above the json writing part? – Jerodev Commented Nov 27, 2017 at 15:07
  • Where do you get this error ? In the writeJsonFile function whose code you don't show ? – Denys Séguret Commented Nov 27, 2017 at 15:07
  • @Jerodev the error came at the line const writeJsonFile = require('write-json-file'); and if i remove the code above i still have the problem – alpheonix Commented Nov 27, 2017 at 15:16
  • Is write-json-file a third party module, or something you've written in the same folder as that JS code? – Andy Commented Nov 27, 2017 at 15:22
  • @Andy write-json-fileis a npm install module that i found here [link] (github./sindresorhus/…) – alpheonix Commented Nov 27, 2017 at 15:27
Add a ment  | 

1 Answer 1

Reset to default 3

Better you use fs module of nodejs. No need to install it.

Here simple example of writing data to JSON file.

const fs = require('fs');
json = {
    one: 1,
    two: 2
}
json = JSON.stringify(json);
fs.writeFile('./foo.json', json, (err) => {
    if (!err) {
        console.log('done');
    }
});
发布评论

评论列表(0)

  1. 暂无评论