我见过一些人询问此错误,但无法确定类似的问题,所以希望有人可能会看到这个问题。
我有一个名为'coinstats'的数据库,其中有一个名为'ticker_data'的文档 - 位于MLAB中。
在express中,我正在尝试创建一条路径来测试将JSON数据渲染回页面。
这是我的模型( 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;
和我的数据:
{
"_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"
}
]
}
这是我试图在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;
我猜测我如何尝试查找数据存在问题,但只是不确定如何构建该路径。
答案 0 :(得分:1)
这里有一些问题。最值得注意的是,你的" btc"查询与全部大写不一致" BTC"值。此外,您的统计信息架构与"和我的数据不对齐:"你的帖子的一部分。您的"和我的数据:"帖子的一部分真的显示了一个包含Stats对象数组的Schema。我无法判断您的Stats对象是否是对另一个Stats架构的引用(例如,它们可以直接查询),或者它们只是一个硬编码对象。
您的实际架构应以两种方式之一定义(未经测试的代码):
//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;
或
//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;
如果是选项1,并且有一个名为" stats"的单独集合,则您的查询应为
Stats.find({symbol: "BTC"})
如果是选项2,那么您将卡住检索整个Stats对象,然后在本地提取btc对象:
Stats.find({_id: "5a7b9124bc378ab20b321466"}, (err, stat) => {
if(err){
err.status = 400
return next(err);
}
res.json(stat.coins.filter(coin -> {return coin.id === 'BTC'}));
});
老实说,这个问题似乎突出了模型/模式/参考的工作方式中的一些误解 - 所以我建议阅读一些基础知识,它应该变得很明显你出错的地方< / p>