sample.json 데이터

 {
 "status":"1",
 "message":"OK",
 "result":[
  {
   "blockNumber":"4473164",
   "timeStamp":"1542787987",
   "hash":"0xac86d44f53a22e4b12853f5f8234f95b831e1521694c4072fd6a97b219ec3cb7",
   "nonce":"17012682",
   "blockHash":"0x4ee9b9d29f78f7aab230613f8827e9e070d5b60b2cd06d61e83b17aaaa5b1e67",
   "transactionIndex":"54",
   "from":"0x81b7e08f65bdf5648606c89998a9cc8164397647",
   "to":"0xff0797d06e8f9897b1d5066c10d9497ed7054a47",
   "value":"1000000000000000000",
   "gas":"21000",
   "gasPrice":"1000000000",
   "isError":"0",
   "txreceipt_status":"1",
   "input":"0x",
   "contractAddress":"",
   "cumulativeGasUsed":"6877760",
   "gasUsed":"21000",
   "confirmations":"104500"
                  }
    ]
}

result속의 데이터만 어떻게 가지고 올까 참 고민 많이 했다.

 

 var fs = require('fs');

console.log(result);
var initialData = fs.readFileSync('sample.json');
var TRList = JSON.parse(initialData);


var Result_data = [];
    for(var i=0; i<TRList.result.length; i++){
        var TR=TRList.result[i];
          var info = {
                  blockNumber: TR.blockNumber,
                  timestamp: TR.timestamp,

                  hash: TR.hash,
                  nonce: TR.nonce,

                  blockHash: TR.blockHash,
                  transactionIndex: TR.transactionIndex,

                  from: TR.from,
                  to: TR.to,

                  value: TR.value,
                  gas: TR.gas,

                  gasPrice: TR.gasPrice,
                  isError: TR.isError,

                  txreceipt_status: TR.txreceipt_status,
                  input: TR.input,

                  contractAddress: TR.contractAddress,
                  cumulativeGasUsed: TR.cumulativeGasUsed,

                  gasUsed: TR.gasUsed,
                  confirmations: TR.confirmations
        };
        Result_data.push(info);
    }
    var result={
        count: Result_data.length, // count
        Result_data: Result_data
    };
console.log(result);

TRList.result 저거 한줄을 못해서 ㅋㅋ 몇시간 동안 머한건지 ,,

 

 root@tkpark-VirtualBox:~/node# nodejs ex4.js
undefined
{ count: 1,
  Result_data:
   [ { blockNumber: '4473164',
       timestamp: undefined,
       hash: '0xac86d44f53a22e4b12853f5f8234f95b831e1521694c4072fd6a97b219ec3cb7',
       nonce: '17012682',
       blockHash: '0x4ee9b9d29f78f7aab230613f8827e9e070d5b60b2cd06d61e83b17aaaa5b1e67',
       transactionIndex: '54',
       from: '0x81b7e08f65bdf5648606c89998a9cc8164397647',
       to: '0xff0797d06e8f9897b1d5066c10d9497ed7054a47',
       value: '1000000000000000000',
       gas: '21000',
       gasPrice: '1000000000',
       isError: '0',
       txreceipt_status: '1',
       input: '0x',
       contractAddress: '',
       cumulativeGasUsed: '6877760',
       gasUsed: '21000',
       confirmations: '104500' } ] }
root@tkpark-VirtualBox:~/node#

 

이제 MongoDB에 집어 넣어 볼가?

var fs = require('fs');
var initialData = fs.readFileSync('sample.json');
var TRList = JSON.parse(initialData);
var mongoose = require('mongoose');
mongoose.connect('mongodb://test:12345@localhost:31337/test',
{
        useNewUrlParser: true
});
var db=mongoose.connection;
var PostSchema = new mongoose.Schema( {
        blockNumber: Number,
        timestamp: Date,
        hash: String,
        nonce: Number,
        blockHash: String,
        transactionIndex: Number,
        from: String,
        to: String,
        value: String,
        gas: Number,
        gasPrice: String,
        isError: Boolean,
        txreceipt_status: Number,
        input: String,
        contractAddress: String,
        cumulativeGasUsed: Number,
        gasUsed: Number,
        confirmations: Number
})
var Post = mongoose.model('Post',PostSchema);
var Result_data = [];
    for(var i=0; i<TRList.result.length; i++){
        var TR=TRList.result[i];
        var post_1 = new Post( {
                blockNumber: TR.blockNumber,
                timestamp: TR.timestamp,
                hash: TR.hash,
                nonce: TR.nonce,
                blockHash: TR.blockHash,
                transactionIndex: TR.transactionIndex,
                from: TR.from,
                to: TR.to,
                value: TR.value,
                gas: TR.gas,
                gasPrice: TR.gasPrice,
                isError: TR.isError,
                txreceipt_status: TR.txreceipt_status,
                input: TR.input,
                contractAddress: TR.contractAddress,
                cumulativeGasUsed: TR.cumulativeGasUsed,
                gasUsed: TR.gasUsed,
                confirmations: TR.confirmations
        });
                post_1.save(function (err) {
                if (err) return handleError(err);
                mongoose.disconnect();
        })
};

다 만들었다 거의 95%정도 완료 ...

save 부분에서 disconnet 해줘야지 백그라운드로 계속 프로세스가 돌지 않고

종료

 

 

 

 

DB에 박히는거 까지 확인

여기서는 조용해서 집중도 잘되고 문제 해결이 잘됨..

 

 

블로그 이미지

iesay

,