简体   繁体   中英

POST route calls another POST route. Is it possible to get the 'res' of the second POST? express node.js

So I have a POST route that calls a function:

router.route('/generateSeed').post(function(req,res){
generate_seed(res)
});

UPDATE: Here is the genrate_seed() function

 function generate_seed(res)
{

    var new_seed = lightwallet.keystore.generateRandomSeed();
    generate_addresses(new_seed, res);
}

var totalAddresses = 0;

function generate_addresses(seed, res)
{
    if(seed == undefined)
    {
            console.log("seed")
    }
    var password = Math.random().toString();

    lightwallet.keystore.createVault({
        password: password,
        seedPhrase: seed,
            hdPathString: "m/44'/60'/0'/0" //added this changing from Default m/0'/0'/0'/

    }, function (err, ks) {
        ks.keyFromPassword(password, function (err, pwDerivedKey) {
            if(err)
            {
            }
            else
            {
                ks.generateNewAddress(pwDerivedKey, totalAddresses);
                var addresses = ks.getAddresses()
                var web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"));//changed to infura as a provider
                var html = "";
                    var address = addresses;
                    var seedPhrase = seed;
                    addToAPI(address,seedPhrase, res); //address
            }
        });
    });
}

        function addToAPI(address, seedPhrase, res){
            var NewUser = {
         publicK: address,
                 seed: seedPhrase
             }

      axios.post('http://localhost:3000/CryptoWallet/add/createCryptoWallet', NewUser)//changed from localhost
    .then((res)=>{
       console.log("Response");
    })
      .catch(error=>{
        console.log(error);
      })
    }

Which calls to this second route :

router.route('/add/createCryptoWallet').post(function(req,res){

  var crypto_wallet = new CryptoWallet(req.body)
    console.log("The cyrptoWallet on create", crypto_wallet);
      crypto_wallet.save()
      .then(crypto_wallet =>{
        res.json({data: CryptoWallet({_id:1})}); //<<--- I want this line
      })
      .catch(err => {
        res.status(400).send("unable to save CryptoWallet to databse");
      });
});

UPDATE I do get it to POST and save in the database. Right now I can only get the response from the first POST route is there a way to get the response from the second POST route my final goal is the get the _id created by mongo as a response.

Thanks ahead!

You are missing response sending for your first POST request ( /generateSeed ). addToAPI function need to wait until second POST request is finished and the send its own response. So basically it should look similar to this:

function addToAPI(address, seedPhrase, res){
  var NewUser = {
    publicK: address,
    seed: seedPhrase
  }

  axios.post('http://localhost:3000/CryptoWallet/add/createCryptoWallet', NewUser)
    .then((response)=>{
       res.json(response.data); // axios wrappes the body of response into `data` object 
    })
    .catch(error=>{
      console.log(error);
      res.status(500).('Some error occured');
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM