it is worth mention that i'm new in node.js , sequelize and axios staff. here is the the requirements: Part 2: Inside your node.js application
Create a model class "BpiModel" with sequeliz to access the your table. => which i have done this.
Create a controller class "BpiModel" inside the controller class create a function to access the api https://api.coindesk.com/v1/bpi/currentprice.json with the AXIOS http library => this part is also done.
Inside the controller class create a function to save the data from the API "bpi": property and store the data inside your database table "bpi". Storing also the current datetime inside the currentDateTime field.
Inside the controller class create a function "getData" to show the data stored inside the database table into JSON format
create a router class that can allow someone from outside to access the "getData" function in JSON format. below are part 1 and part 2 codes and result.
const axios = require('axios').default; const getData = () => {
axios.get('https://api.coindesk.com/v1/bpi/currentprice.json') .then(res => { const myTxt = res.data; let myData =myTxt.bpi; console.log (myData); }) } getData();
i would like to convert getData() to a format where i can easily insert it in to mysql db,any help will be highly appreciated.
The output is:
{
USD: {
code: 'USD',
symbol: '$',
rate: '19,042.3894',
description: 'United States Dollar',
rate_float: 19042.3894
},
GBP: {
code: 'GBP',
symbol: '£',
rate: '15,911.6683',
description: 'British Pound Sterling',
rate_float: 15911.6683
},
EUR: {
code: 'EUR',
symbol: '€',
rate: '18,550.0675',
description: 'Euro',
rate_float: 18550.0675
}
}
and my model is as follows where with postman everything is working:
module.exports = (sequelize, Sequelize) => {
const Bpi = sequelize.define("bpi", {
code: {
type: Sequelize.STRING,
allowNull: false
},
symbol: {
type: Sequelize.STRING,
allowNull: true
},
rate: {
type: Sequelize.STRING,
allowNull: true
},
description:
{
type: Sequelize.STRING,
allowNull: true
},
rate_float: {
type: Sequelize.FLOAT,
allowNull: true
},
currentDateTime: {
type: Sequelize.DATE,
allowNull: true
}
});
return Bpi;
};
if you would like more explanation do not hesitate to hit the reply button.