Reputation: 1
Inspired by this post build a cryptocurrency comparison site- with vuejs
I want to write a single-page application using the total.js platform to get the coins list from the "coinmarketcap.com" api, and store the response into the database. I've done the following. But I get an error while storing the received information. How can I save the received json file to the database or file system and use it somewhere else?
1- I have written a file called "coins.js” inside the "models" folder containing the following code
// For importing
const Fs = require('fs');
NEWSCHEMA('UsdPrice').make(function(schema) {
schema.define('price', Number);
schema.define('volume_24h', Number);
schema.define('percent_change_1h', Number);
schema.define('percent_change_24h', Number);
schema.define('percent_change_7d', Number);
schema.define('market_cap', Number);
schema.define('last_updated', Date);
});
NEWSCHEMA('Coin').make(function(schema) {
schema.define('id', Number);
schema.define('name', 'String(50)');
schema.define('symbol', 'String(40)');
schema.define('slug', 'String(50)');
schema.define('num_market_pairs', Number);
schema.define('date_added', Date);
schema.define('tags', '[String]');
schema.define('max_supply', Number);
schema.define('circulating_supply', Number);
schema.define('total_supply', Number);
schema.define('platform', 'String(20)');
schema.define('cmc_rank', Number);
schema.define('last_updated', Date);
schema.define('quote', '[Object]');
schema.define('USD', '[UsdPrice]');
schema.setSave(function($) {
var model = $.model;
var coin = U.extend({}, model.$clean());
// Extends model
coin.dateadded = F.datetime;
NOSQL('coins').insert(JSON.parse(JSON.stringify(coin)));
});
});
2- I have written a file called “default.js” inside the “controllers” folder containing the following code
exports.install = function() {
ROUTE('/', viwe_index);
ROUTE('POST /api/coins/ *coin --> @save');
};
function viwe_index() {
var self = this;
RESTBuilder.make(function(builder) {
builder.url('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest? start=1&limit=2&convert=USD');
builder.header('X-CMC_PRO_API_KEY', 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c');
builder.accept('application/json');
builder.header('Accept-Encoding', 'gzip');
builder.GET().plain().exec(function(err, response) {
//console.log(response);
AJAX('POST /api/coins/', 'coin', function(response){
response.$save();
});
});
}, function() {
self.view('index');
});
}
3- I have written a file called “index.html” inside the “views” folder containing the following code
@{layout('')}
<!DOCTYPE html>
<html>
<head>
<title>CryptoCompare</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta name="format-detection" content="telephone=no"/>
<meta name="viewport" content="width=1024, user-scalable=yes" />
<meta name="robots" content="all,follow" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style type="text/css">
body { padding: 50px; margin: 0; font:normal 12px Arial; color: gray }
a { float:left; margin: 0 5px 0 0; font-size: 13px; }
p { background-color: #F0F0F0; padding: 5px; font:bold 15px Arial; }
</style>
</head>
<body>
<div>CryptoCompareTotal index</div>
</body>
</html>
when i uncomment the line "//console.log(response);" in "default.js" i get the bellow response , but when i comment it, i get the error.
{
"status": {
"timestamp": "2019-09-23T11:17:47.434Z",
"error_code": 0,
"error_message": null,
"elapsed": 10,
"credit_count": 1,
"notice": null
},
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"num_market_pairs": 8058,
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"max_supply": 21000000,
"circulating_supply": 17952637,
"total_supply": 17952637,
"platform": null,
"cmc_rank": 1,
"last_updated": "2019-09-23T11:16:35.000Z",
"quote": {
"USD": {
"price": 9967.31536844,
"volume_24h": 12606470860.563,
"percent_change_1h": -0.0940096,
"percent_change_24h": -0.677549,
"percent_change_7d": -3.23074,
"market_cap": 178939594674.12457,
"last_updated": "2019-09-23T11:16:35.000Z"
}
}
},
{
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"slug": "ethereum",
"num_market_pairs": 5641,
"date_added": "2015-08-07T00:00:00.000Z",
"tags": [
"mineable"
],
"max_supply": null,
"circulating_supply": 107859213.999,
"total_supply": 107859213.999,
"platform": null,
"cmc_rank": 2,
"last_updated": "2019-09-23T11:17:26.000Z",
"quote": {
"USD": {
"price": 210.843252214,
"volume_24h": 7065823660.95811,
"percent_change_1h": 0.108406,
"percent_change_24h": -0.423833,
"percent_change_7d": 9.19434,
"market_cap": 22741387460.794956,
"last_updated": "2019-09-23T11:17:26.000Z"
}
}
}
]
}
Upvotes: 0
Views: 365
Reputation: 65
Please read response, you have array of objects in response.data.
Controller
exports.install = function() {
ROUTE('/', view_index);
// or
// ROUTE('/');
};
function view_index() {
var self = this;
RESTBuilder.make(function(builder) {
builder.url('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=2&convert=USD');
builder.header('X-CMC_PRO_API_KEY', '07c1f1d1-681a-467e-8558-923da4d815c2');
builder.accept('application/json');
builder.header('Accept-Encoding', 'gzip');
builder.exec(function(err, response) {
var data = response.data;
data.wait(function(item, next) {
// Schema save
$SAVE('Coin', item);
// Or save directly to NOSQL
// NOSQL('coins').insert(item);
next();
}, function() {
self.view('index');
});
});
});
}
Schema
schema.setSave(function($) {
var model = $.clean();
// Extends model
model.dateadded = F.datetime;
NOSQL('coins').insert(model);
});
Upvotes: 0
Reputation: 65
Firstly AJAX
is jComponent function, so frontend library, not a backend total.js library.
Please try this code, I don't have API key, so I can't try.
Controller:
exports.install = function() {
ROUTE('/', view_index);
};
function view_index() {
var self = this;
RESTBuilder.make(function(builder) {
builder.url('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest? start=1&limit=2&convert=USD');
builder.header('X-CMC_PRO_API_KEY', 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c');
builder.accept('application/json');
builder.header('Accept-Encoding', 'gzip');
builder.exec(function(err, response) {
// Here you could change response to your model
var model = response;
// Save to database without callback
// For usage please read docs: https://docs.totaljs.com/latest/en.html#api~global~%24SAVE
$SAVE('Coin', model);
self.view('index');
});
});
}
Part of schema file:
schema.setSave(function($) {
var model = $.clean();
// Extends model
coin.dateadded = F.datetime;
NOSQL('coins').insert(model);
});
Upvotes: 0