node.js - can not insert document (record) into mongodb -


i developing project in mean stack , need import countries, states , cities following code.
can import countries , states json files cities can import first document (record) instead of all.

route.js

router     .route('/api/user/loadcity')     .get(         function(req, res, next) {             var fs = require("fs");             fs.readfile('/home/user7/downloads/city.json', 'utf8', function (err,data) {               data = json.parse(data);               for(var = 0; < data.length; i++) {                console.log(data[i].city_name);                 var newcity = new city({                     id:data[i].id,                     country_id : data[i].country_id,                     state_id : data[i].state_id,                     city_name : data[i].city_name,                     is_active : data[i].is_active                 });                 newcity.save(function (err) {                     if(err){                         console.log(err);                     }                 });               }             });         });   router     .route('/api/user/loadcountry')     .get(         function(req, res, next) {             var fs = require("fs");             fs.readfile('/home/user7/downloads/country.json', 'utf8', function (err,data) {               data = json.parse(data);                for(var = 0; < data.length; i++) {                //console.log(data[i].city_name);                 var newcountry = new country();                 newcountry.id = data[i].id;                 newcountry.country_name = data[i].country_name;                 newcountry.country_code = data[i].country_code;                 newcountry.country_flag = data[i].country_flag;                 newcountry.is_active = data[i].is_active;                 newcountry.save(function (err) {                     if(err) console.log(err);                 });               }             });         });    router     .route('/api/user/loadstate')     .get(         function(req, res, next) {             var fs = require("fs");             fs.readfile('/home/user7/downloads/state.json', 'utf8', function (err,data) {               data = json.parse(data);                for(var = 0; < data.length; i++) {                console.log(data[i].state_name);                 var newstate = new state({                     id:data[i].id,                     country_id : data[i].country_id,                     state_name : data[i].state_name,                     is_active : data[i].is_active                 });                 newstate.save(function (err) {                     if(err) console.log(err);                 });               }             });         }); 

form above code loadcountry , loadstate routing code working properly, loadcity routing code can insert 1 document (record).

dependencies

country.json  =>          file size 48.9 kb (48,883 bytes)         total records 252 state.json    =>          file size 362.5 kb (3,62,478 bytes)         total records 2800 city.json     =>        file size 21.0 mb (2,09,63,600 bytes)         total records 142987  "express"  => "version": "4.13.4", "mongoose" => "version": "4.4.4", "mongodb"  => "version": "2.4.9", "os"  => "ubuntu 14.04 lts 32bit", 

anybody can me insert these cities.

i not sure how country , state working fine. save being i/o operation in mongodb async in nature , loop sync.the values inserted last item in array.

you use closure solve one..

router     .route('/api/user/loadcity')     .get(         function(req, res, next) {             var fs = require("fs");             fs.readfile('/home/user7/downloads/city.json', 'utf8', function(err, data) {                 data = json.parse(data);                 (var = 0; < data.length; i++) {                    (function(i){                     console.log(data[i].city_name);                     var newcity = new city({                         id: data[i].id,                         country_id: data[i].country_id,                         state_id: data[i].state_id,                         city_name: data[i].city_name,                         is_active: data[i].is_active                     });                     newcity.save(function(err) {                         if (err) {                             console.log(err);                         }                     });                   })(i);                                       }             });         }); 

using async library

  async.foreach(data, function(data, callback) {    });      

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -