javascript - How to write 500.000 words in one line with node.js fs module without lines to be break? -
i want write format
1 {"index": {"_id": 0}} 2 { "age_groups" : ["-kmqn3sh7_ ........100000 words in sinle line 3 {"index": {"_id": 1}} 4 { "age_groups" : ["-kmqn3sh7_ ........100000 words in sinle line 5 {"index": {"_id": 2}} 6 { "age_groups" : ["-kmqn3sh7_ ........100000 words in sinle line 7 {"index": {"_id": 3}} 8 { "age_groups" : ["-kmqn3sh7_ ........100000 words in sinle line
but format after write file
{"index": {"_id": 0}} { "age_groups" : ["-kmqn3sh7_p73cwzrsmd","-kmqn6aze7lroyh_ccpp","-"-kmqnklhhro0annagybq"], "needs": ["-km5n7vj_x5j2yj9ag6c","-km5ncklpde8c4nfq6kg"], "categories": ["-kmctr9mdf_cvwts3b74","-kmcter6woj-xm1jpns4"], "tags": , "title": "bepanthol - intensive Κρέμα Προσώπου & Ματιών - 50ml", "code": "113829", "brand": "bepanthol", "price_bought": "0", "price_sell": "16.05", "weight": "50ml", "description": "<h3><span style="font-size:12px"><strong>bepanthol</strong></span></h3> <h3><span style="font-size:12px"><strong>intensive κρέμα πρ&oma;η (50ml)</strong></span></h3> <p><span style="font-size:12px">σύνθεση με προβιταμίνη β5 &kappaaf;:</span></p> <ul> <li><span style="font-size:12px">ενισχύει και διενυδατωμένο</span></li> <li><span style="font-size:12px">ευνοεί την απο&kalpha;ς</span></li> <li><span style="font-size:12px">διατηρεί το δέρμα. </span></li> </ul>
can tell me how can achieve that?
this code :
const fs = require('fs'); const jsonfile = require('jsonfile'); var logger = fs.createwritestream('./exports/log1.txt', { flags: 'a' // 'a' means appending (old data preserved) }) function convertproducts_fs() { var selectedfile = document.getelementbyid('inp_products_fs').files[0]; const filepath = selectedfile.path; const filetoread = filepath; jsonfile.readfile(filetoread, function(err, data) { // console.dir(data) let counter = 0; (let key in data) { if (data.hasownproperty(key)) { let element = data[key]; // olo element // console.log(data[key]); let schema = {}; schema.age_groups = []; schema.needs = []; schema.categories = []; schema.tags = []; schema.title = element.title; schema.code = element.custom_id; schema.brand = element.brand; schema.price_bought = element.bought_price; schema.price_sell = element.sell_price; schema.weight = element.weight; schema.description = element.description; schema.uses = element.uses; schema.created = element.timestamp; schema.updated = element.timestamp; schema.image_url = ''; schema.added_by = 'admin@gmail.com'; schema.status = 'inactive'; (let _key1 in element.age_groups) { schema.age_groups.push(_key1); } (let _key2 in element.needs) { schema.needs.push(_key2); } (let _key3 in element.final_category) { schema.categories.push(_key3); } schema.created = element.timestamp; schema.updated = element.timestamp; // products.push({ // "index": { // "_id": counter // } // }) console.log(counter); // console.log(schema); // products.push(schema) let age_groups = json.stringify(schema.age_groups); let needs = json.stringify(schema.needs); let categories = json.stringify(schema.categories); logger.write(`{"index": {"_id": ${counter}}}`) // append string file logger.write('\r\n') logger.write(`{ "age_groups" : ${age_groups}, "needs": ${needs}, "categories": ${categories}, "tags": ${schema.tags}, "title": "${schema.title}", "code": "${schema.code}", "brand": "${schema.brand}", "price_bought": "${schema.price_bought}", "price_sell": "${schema.price_sell}", "weight": "${schema.weight}", "description": "${schema.description}", "uses": "${schema.uses}" }`) // append string file logger.write('\r\n') } counter++; } // console.log(brands); console.log('ok'); logger.end(); }); }
lorem ipsum dummy text of printing , typesetting industry. lorem ipsum has been industry's standard dummy text ever since 1500s, when unknown printer took galley of type , scrambled make type specimen book. has survived not 5 centuries, leap electronic typesetting, remaining unchanged. popularised in 1960s release of letraset sheets containing lorem ipsum passages, , more desktop publishing software aldus pagemaker including versions of lorem ipsum.
if i've understood correctly problems start here:
"tags": ${schema.tags}
you aren't passing value through json.stringify
way did earlier values it's getting included unencoded, including newline characters within it.
there several other unencoded values on line.
compare how output categories
:
let categories = json.stringify(schema.categories); "categories": ${categories}
to how output tags
:
"tags": ${schema.tags}
all said, easier avoid building json in such piecemeal fashion , use:
logger.write(json.stringify(schema));
if want output properties can use:
logger.write(json.stringify(schema, ['age_groups', 'needs', /* etc. */]));
Comments
Post a Comment