如何将 MongoDB 数据用于 D3 和 Node?
How do I use MongoDB data with D3 and Node?
使用 Node、Express、Mongoose 和 MongoDB,我正在尝试向 MongoDB 发送 GET
请求,然后将数据用作 D3 图形的对象.我在单独的 js 文件中有图形代码。如何将数据发送到该文件?
以下是我正在使用的基本模式:
var hikeSessionSchema = new mongoose.Schema({
hike_name: {type: String, required: true},
hike_date: {type: String, required: true},
mileage: {type: Number, required: true},
duration: {type: String, required: true},
elevation_gain: {type: Number, required: true},
city: {type: String, required: true},
location: {type: String, required: true},
notes: String
})
var hikerSchema = new mongoose.Schema({
username: {type: String, required: true},
log: [hikeSessionSchema]
})
var HikeSession = mongoose.model('HikeSession', hikeSessionSchema)
var Hiker = mongoose.model('Hiker', hikerSchema)
所以我想通过ID找到用户,然后得到用户的数组HikeSessions
。我想将该数据移动到 js 文档以制作 d3 图表(我已经在 CodePen 上制作了 d3 应用程序,但之前将其连接到 Google Sheets)。
我认为GET
请求应该是这样的,但我不知道如何处理result
。路径可能会改变:
app.get('/:id', (req, res) => {
const id = req.params.id;
Hiker.findById(id)
.then(result => {
//What goes here?
})
.catch(err => {
console.log(err);
})
}
将数据输入 d3 文档后,我不确定在渲染它时 运行 会遇到哪些挑战。
第 1 步:
在 d3.js
中定义并导出 d3_generator(input)
函数:
const d3_generator = (input) => {
/// (Your logic)
}
module.exports = { d3_generator };
步骤 2
在控制器中从 d3.js
导入 d3_generator
函数,并在从 MongoDB
获得结果后使用它:
const { d3_generator } = require('./d3');
...
app.get('/:id', (req, res) => {
const id = req.params.id;
Hiker.findById(id)
.then(result => {
let generated_d3 = d3_generator(result.log);
})
.catch(err => {
console.log(err);
})
}
使用 Node、Express、Mongoose 和 MongoDB,我正在尝试向 MongoDB 发送 GET
请求,然后将数据用作 D3 图形的对象.我在单独的 js 文件中有图形代码。如何将数据发送到该文件?
以下是我正在使用的基本模式:
var hikeSessionSchema = new mongoose.Schema({
hike_name: {type: String, required: true},
hike_date: {type: String, required: true},
mileage: {type: Number, required: true},
duration: {type: String, required: true},
elevation_gain: {type: Number, required: true},
city: {type: String, required: true},
location: {type: String, required: true},
notes: String
})
var hikerSchema = new mongoose.Schema({
username: {type: String, required: true},
log: [hikeSessionSchema]
})
var HikeSession = mongoose.model('HikeSession', hikeSessionSchema)
var Hiker = mongoose.model('Hiker', hikerSchema)
所以我想通过ID找到用户,然后得到用户的数组HikeSessions
。我想将该数据移动到 js 文档以制作 d3 图表(我已经在 CodePen 上制作了 d3 应用程序,但之前将其连接到 Google Sheets)。
我认为GET
请求应该是这样的,但我不知道如何处理result
。路径可能会改变:
app.get('/:id', (req, res) => {
const id = req.params.id;
Hiker.findById(id)
.then(result => {
//What goes here?
})
.catch(err => {
console.log(err);
})
}
将数据输入 d3 文档后,我不确定在渲染它时 运行 会遇到哪些挑战。
第 1 步:
在 d3.js
中定义并导出 d3_generator(input)
函数:
const d3_generator = (input) => {
/// (Your logic)
}
module.exports = { d3_generator };
步骤 2
在控制器中从 d3.js
导入 d3_generator
函数,并在从 MongoDB
获得结果后使用它:
const { d3_generator } = require('./d3');
...
app.get('/:id', (req, res) => {
const id = req.params.id;
Hiker.findById(id)
.then(result => {
let generated_d3 = d3_generator(result.log);
})
.catch(err => {
console.log(err);
})
}