Cant get margin in nodejs

mijanur
mijanur edited January 2021 in API clients
when trying to get margins using getMargins() geting response as [object Object] when trying to log the object as console.log(response[0]) its showing undefined, can anybody explain this issue ?
  • mijanur
    getting response as [object Object] but cant iterate
  • mijanur
    historical data also not loading, there is no response, is there any solution available ?
  • mijanur
    getting error on range 2021-01-20 09:15:00 to 2021-01-25 15:20:00

    error message :
    Get Rms Limits Entity Response : Request not registered
  • rakeshr
    rakeshr edited January 2021
    @mijanur
    You seem to be fetching this during the BOD process. You can go through this thread to know more about BOD timings.
    Also, you can check all node js API fetch examples here.
  • mijanur
    mijanur edited January 2021
    @rakeshr
    executed the codes now, but still same result, just not getting the error message, not getting the margins or historical data
  • rakeshr
    @mijanur
    Can you paste your code here? You don't see to be catching error properly.
    If you use this example, it should show the nested dictionary, as shown in documentation here.
  • mijanur
    @rakeshr
    here is my codes
    var KiteConnect = require("kiteconnect").KiteConnect;
    const express = require('express')
    const app = express()
    const port = 8081

    var apiKey , apiSec, accTok, pubTok
    var banknifty = '12680706'

    app.get('/', (req, res) => {
    res.send("server started")
    })

    app.get('/api/zerodha/:userId', (req, res) => {
    var reqToken = req.query.request_token
    console.log(reqToken)
    console.log(req.params.userId)
    reqToken != null && genSession(reqToken, apiSec)
    res.send(`Request token ${reqToken} and api secret ${apiSec}`)
    })

    app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
    })

    function testApi(api, sec){
    apiKey = api
    apiSec = sec
    global.kc = new KiteConnect({
    api_key: apiKey
    });
    console.log(kc.getLoginURL())
    }



    function genSession(request_token, api_secret){
    kc.generateSession(request_token, api_secret)
    .then(function(response) {
    console.log(response)
    accTok = response['access_token']
    pubTok = response['public_token']
    kc.setAccessToken(accTok)
    init()
    kc.getHistoricalData(banknifty,'5minute', '2021-01-20 09:15:00', '2021-01-25 15:20:00')
    .then(
    console.log(`getting historical data`),
    function (dat) {
    console.log(dat)
    }
    )
    .catch(function(err) {
    console.log(err);
    })
    })
    .catch(function(err) {
    console.log(err);
    });
    }



    function init() {
    // Fetch equity margins.
    // You can have other api calls here.
    console.log(`getting margin`)
    kc.getMargins()
    .then(function(response) {
    // You got user's margin details.
    console.log(`got it : ${response}`)
    console.log(`got it : ${response[0]}`)
    }).catch(function(err) {
    // Something went wrong.
    console.log(err)
    });
    }

    testApi(my_api_key,my_api_secret)
  • rakeshr
    @mijanur
    // You got user's margin details.
    console.log(`got it : ${response}`)
    Your console statement is wrong. This is an incorrect way to concatenate an object to a string. You need to separate a string and an object by a comma. The correct way would be:
    console.log("got it :", response);
    console.log(`got it : ${response[0]}`)
    Also, you need to use dot operator(.) to extract required field from an object. Eg:
    console.log("Get equity margin:", response.equity);
  • mijanur
    mijanur edited January 2021
    @rakeshr
    this is called template string, and its right approach of debugging, my problem is not about concatenating , I am not getting value

    I am getting in console

    getting margin
    getting historical data
    got it : [object Object]
    got it : undefined


    undefined for response[0]

    If trying response.equity
    also getting [object Object]

    what can I do with this object if I cant iterate ?

    also there is no response in getMargins() function
  • rakeshr
    @mijanur
    We don't assist on coding. getMargins() call is working absolutely fine at our end. It's printing the desired response per output structure. You need to check your code.
    function init() {
    // Fetch complete margins.
    console.log(`getting margin`)
    kc.getMargins()
    .then(function(response) {
    // user's margin details.
    console.log("got it :", response);
    // Extract Equity Margin
    console.log("Get equity margin :", response.equity);
    }).catch(function(err) {
    // Something went wrong.
    console.log(err)
    });
    }
  • mijanur
    mijanur edited January 2021
    @rakeshr
    Oh so sorry, I understand the mistake,
    but still not getting historicaldata

    I want to get todays historical data in 5minutes what should i pass as parameters ? can you give example codes ?
  • mijanur
    getting the data
This discussion has been closed.