GithubHelp home page GithubHelp logo

Hey I am getting "TypeError: Cannot read property 'publicKey' of undefined" on calling get request in NodeJS project. about cryptochain HOT 4 CLOSED

15dkatz avatar 15dkatz commented on July 22, 2024
Hey I am getting "TypeError: Cannot read property 'publicKey' of undefined" on calling get request in NodeJS project.

from cryptochain.

Comments (4)

 avatar commented on July 22, 2024

Here is the index.js File

const bodyParser = require('body-parser');
const express = require('express');
const request = require('request');
//const path = require('path');
const Blockchain = require('./blockchain');
const PubSub = require('./app/pubsub');
const TransactionPool = require('./wallet/transaction-pool');
const Wallet = require('./wallet');

const app = express();
const blockchain = new Blockchain();
const transactionPool = new TransactionPool();
const wallet = new Wallet();
const pubsub = new PubSub({ blockchain, transactionPool });
//const transactionMiner = new TransactionMiner({ blockchain, transactionPool, wallet, pubsub });

const DEFAULT_PORT = 3000;
const ROOT_NODE_ADDRESS = `http://localhost:${DEFAULT_PORT}`;
 
app.use(bodyParser.json());

app.get('/api/blocks', (req, res) => {
    res.json(blockchain.chain);
});

app.post('/api/mine', (req, res) => {
    const { data } = req.body; 

    blockchain.addBlock({ data });
    
    pubsub.broadcastChain();
     
    res.redirect('/api/blocks');
});

app.post('/api/transact', (req, res) => {
    const { amount, recipient } = req.body;

    let transaction = transactionPool
        .existingTransaction({ inputAddress: wallet.publicKey });

    try {
        if (transaction) {
            transaction.update({ senderWallet: wallet, recipient, amount });
        } else {
            transaction = wallet.createTransaction({ recipient, amount });

        }
    } catch(error) {
        return res.status(400).json({ type: 'error', message: error.message });
    }

    transactionPool.setTransaction(transaction);

    pubsub.broadcastTransaction(transaction);

    console.log('transactionPool()', transactionPool);
  
    // let transaction = transactionPool
    //     .existingTransaction({ inputAddress: wallet.publicKey });
  
    res.json({ type: 'success', transaction });
});

app.get('/api/transaction-pool-map', (req, res) => {
    res.json(transactionPool.transactionMap);
});

const syncChains = ()=> {
    request({ url: `${ROOT_NODE_ADDRESS}/api/bocks` }, (error, response, body)=> {
        if (!error && response.statusCode === 200) {
            const rootChain = JSON.parse(body);
      
            console.log('replace transaction pool map on a sync with', rootChain);
            blockchain.replaceChain(rootChain);
        }
    });
} 

let PEER_PORT;

if (process.env.GENERATE_PEER_PORT === 'true') {
    PEER_PORT = DEFAULT_PORT + Math.ceil(Math.random() * 1000);
}

const PORT = PEER_PORT || DEFAULT_PORT;
app.listen(PORT, () => {
    console.log(`listening at localhost: ${PORT}`);

    if (PORT !== DEFAULT_PORT) {
        syncChains();
    }
});

when I try to send a get request using postman by following command
http://localhost:3000/api/transaction-pool-map
I got the above error.

from cryptochain.

ironic833 avatar ironic833 commented on July 22, 2024

How did you fix this?

from cryptochain.

MikeBelyayaev avatar MikeBelyayaev commented on July 22, 2024

How did you fix this?

yeah. how?

from cryptochain.

ironic833 avatar ironic833 commented on July 22, 2024

Fixed this by removing the mine block endpoint. Once being able to send proper transactions is implemented mining blocks is apart of that process. Do not strobe the mine-blocks endpoint via postman was my fix

from cryptochain.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.