Reputation: 1183
I've seen a few people asking about this error, but couldn't identify a similar issue, so hoping someone might see the issue.
I have a database called 'coinstats' with one document called 'ticker_data'--housed in MLAB.
In express, i'm trying to create a route to test render the JSON data back to a page.
Here is my Model (Stats.js)
const mongoose = require('mongoose');
const StatsSchema = new mongoose.Schema({
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
})
const Stats = mongoose.model('Stats', StatsSchema);
module.exports = Stats;
and my data:
{
"_id": {
"$oid": "5a7b9124bc378ab20b321466"
},
"coins": [
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "8192.48",
"price_btc": "1.0",
"24h_volume_usd": "10944200000.0",
"market_cap_usd": "138055675030",
"available_supply": "16851512.0",
"total_supply": "16851512.0",
"max_supply": "21000000.0",
"percent_change_1h": "0.86",
"percent_change_24h": "15.76",
"percent_change_7d": "-19.22",
"last_updated": "1518024265"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "825.916",
"price_btc": "0.100574",
"24h_volume_usd": "4620530000.0",
"market_cap_usd": "80502659260.0",
"available_supply": "97470759.0",
"total_supply": "97470759.0",
"max_supply": null,
"percent_change_1h": "1.44",
"percent_change_24h": "15.36",
"percent_change_7d": "-25.51",
"last_updated": "1518024253"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "3",
"price_usd": "0.776055",
"price_btc": "0.00009450",
"24h_volume_usd": "1423720000.0",
"market_cap_usd": "30273296997.0",
"available_supply": "39009215838.0",
"total_supply": "99992725510.0",
"max_supply": "100000000000",
"percent_change_1h": "0.22",
"percent_change_24h": "10.1",
"percent_change_7d": "-31.37",
"last_updated": "1518024241"
},
{
"id": "bitcoin-cash",
"name": "Bitcoin Cash",
"symbol": "BCH",
"rank": "4",
"price_usd": "1014.04",
"price_btc": "0.123481",
"24h_volume_usd": "846536000.0",
"market_cap_usd": "17192667935.0",
"available_supply": "16954625.0",
"total_supply": "16954625.0",
"max_supply": "21000000.0",
"percent_change_1h": "1.72",
"percent_change_24h": "13.56",
"percent_change_7d": "-31.79",
"last_updated": "1518024258"
},
{
"id": "cardano",
"name": "Cardano",
"symbol": "ADA",
"rank": "5",
"price_usd": "0.354359",
"price_btc": "0.00004315",
"24h_volume_usd": "820347000.0",
"market_cap_usd": "9187490789.0",
"available_supply": "25927070538.0",
"total_supply": "31112483745.0",
"max_supply": "45000000000.0",
"percent_change_1h": "-0.2",
"percent_change_24h": "8.88",
"percent_change_7d": "-30.48",
"last_updated": "1518024260"
},
{
"id": "litecoin",
"name": "Litecoin",
"symbol": "LTC",
"rank": "6",
"price_usd": "150.66",
"price_btc": "0.0183462",
"24h_volume_usd": "967680000.0",
"market_cap_usd": "8303348407.0",
"available_supply": "55113158.0",
"total_supply": "55113158.0",
"max_supply": "84000000.0",
"percent_change_1h": "1.55",
"percent_change_24h": "17.03",
"percent_change_7d": "-7.16",
"last_updated": "1518024242"
},
{
"id": "neo",
"name": "NEO",
"symbol": "NEO",
"rank": "7",
"price_usd": "111.903",
"price_btc": "0.0136266",
"24h_volume_usd": "694650000.0",
"market_cap_usd": "7273695000.0",
"available_supply": "65000000.0",
"total_supply": "100000000.0",
"max_supply": null,
"percent_change_1h": "1.6",
"percent_change_24h": "25.29",
"percent_change_7d": "-22.08",
"last_updated": "1518024251"
},
{
"id": "stellar",
"name": "Stellar",
"symbol": "XLM",
"rank": "8",
"price_usd": "0.364085",
"price_btc": "0.00004434",
"24h_volume_usd": "237845000.0",
"market_cap_usd": "6710864323.0",
"available_supply": "18432136240.0",
"total_supply": "103669352049",
"max_supply": null,
"percent_change_1h": "0.47",
"percent_change_24h": "9.22",
"percent_change_7d": "-31.29",
"last_updated": "1518024244"
},
{
"id": "eos",
"name": "EOS",
"symbol": "EOS",
"rank": "9",
"price_usd": "8.27388",
"price_btc": "0.00100753",
"24h_volume_usd": "890299000.0",
"market_cap_usd": "5408422632.0",
"available_supply": "653674290.0",
"total_supply": "900000000.0",
"max_supply": "1000000000.0",
"percent_change_1h": "1.07",
"percent_change_24h": "11.13",
"percent_change_7d": "-29.0",
"last_updated": "1518024256"
}
]
}
and here is where i am trying to create the route in Express:
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const Stats = require("../models/stats.js");
/* GET coin stats. */
router.get('/', (req, res, next) => {
Stats.collection("ticker_data").find({"coins": {symbol: "btc"}}, 'name' (err, stats) => {
if(err){
err.status = 400
return next(err);
}
res.json(stats);
});
})
module.exports = router;
I'm guessing there's an issue with how i am attempting to find the data, but just unsure on how to structure that route.
Upvotes: 0
Views: 140
Reputation: 2379
There are a few issues, here. Most notably, your "btc" query does not align with the all-caps "BTC" value. Additionally, your Stats schema does not align with the "and my data:" part of your post. Your "and my data:" part of the post really shows a Schema that contains an Array of Stats objects. I can't tell whether your Stats objects are a reference to another Stats schema (eg. they can be queried directly), or if they are just a hardcoded Object).
Your actual schema should be defined in one of two ways (untested code):
//Option 1.
//Define a TickerData schema in addition to your Stats schema.
//Your Ticker schema will contain an array with references to your Stats objects.
//For most use-cases, I suspect this is the better option.
// TickerData.js
const mongoose = require('mongoose');
const Stats = require('Stats');
const TickerDataSchema = new mongoose.Schema({
id: String,
coins: [{ type: mongoose.Schema.ObjectId, ref: 'Stats' }]
})
const TickerData = mongoose.model('TickerData', TickerDataSchema);
module.exports = TickerData;
or
//Option 2. Array of hardcoded coin objects
//Replace your Stats schema with this:
const mongoose = require('mongoose');
const StatsSchema = new mongoose.Schema({
id: String,
coins: [{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
},
{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
},
{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
}, {
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
},
{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
},
{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
},
{
id: String,
name: String,
symbol: String,
rank: Number,
price_usd: Number,
price_btc: Number,
"24h_volume_usd": Number,
market_cap_usd: Number,
available_supply: Number,
total_supply: Number,
max_supply: Number,
percent_change_1h: Number,
percent_change_24h: Number,
percent_change_7d: Number,
last_updated: Number,
}]
});
const Stats = mongoose.model('Stats', StatsSchema);
module.exports = Stats;
If it is option 1, and there is a separate collection called "stats", then your query should be
Stats.find({symbol: "BTC"})
If it is option 2, then you're stuck retrieving the whole Stats object and then extracting the btc object locally:
Stats.find({_id: "5a7b9124bc378ab20b321466"}, (err, stat) => {
if(err){
err.status = 400
return next(err);
}
res.json(stat.coins.filter(coin -> {return coin.id === 'BTC'}));
});
Honestly, this question appears to highlight a few misunderstandings in the way that models/schemas/references work -- so I would suggest reading up on some of the basics and it should become apparent where you're going wrong
Upvotes: 1