@vince I still can’t display my smart related records.
I added a smart field schedules
in my payment collection like this:
collection('payment', {
actions: [cancelPaymentSchedule, sendScheduleReminderEmail],
fields: [
{
field: 'schedules',
type: ['Object'],
reference: 'payment.schedules'
}
],
segments: []
})
I am not the sure the reference payment.schedules
is good nor the type [Object']
.
Then, I created a smart collection like this:
collection('schedule', {
onlyForRelationships: true,
isSearchable: false,
fields: [
{
field: 'method',
type: 'Number'
},
{
field: 'amount',
type: 'Number'
},
{
field: 'currency',
type: 'Number'
},
{
field: 'dueDate',
type: 'Date'
},
{
field: 'status',
type: 'Number'
},
{
field: '_id',
type: 'String'
}
]
})
Then, I created a route:
router.get('/payment/:paymentId/relationships/schedules', handleGetPaymentSchedules)
Here is the handleGetPaymentSchedules
function:
const { RecordSerializer } = require('forest-express-mongoose')
async function handleGetPaymentSchedules(request, response) {
const { paymentId } = request.params
const recordSerializer = new RecordSerializer({ modelName: 'schedule' })
const payment = await Payment.findById(paymentId)
const schedules = payment.schedules.map(schedule => {
return {
_id: schedule._id,
method: schedule.method,
amount: schedule.amount,
status: schedule.status,
dueDate: schedule.dueDate,
currency: schedule.currency
}
}
const serializedSchedules = await recordSerializer.serialize(schedules, { count: schedules.length })
response.send(serializedSchedules)
}
Should RecordSerializer
be imported from forest-express-mongoose'
or from forest-express-sequelize'
? I am on MongoDB.
I get this result so far:
and my logs after I clicked on the schedules
link in the payment recored related data:
api_forest_1 | info: ┏ GET /forest/payment/5f7d6edc5f2ec6015f124cf2/relationships/schedules?fields%5Bpayment%5D=_id%2CorderId%2CcreatedAt%2CupdatedAt&fields%5BorderId%5D=orderNumber&page%5Bnumber%5D=1&page%5Bsize%5D=15&timezone=Europe%2FLondon&sort=-_id 304 15ms +13ms
api_forest_1 | info: ┃ [ 1] {
api_forest_1 | info: ┃ [ 2] http: {
api_forest_1 | info: ┃ [ 3] request: {
api_forest_1 | info: ┃ [ 4] method: 'GET',
api_forest_1 | info: ┃ [ 5] url: 'http://api-forest.dev.localhost/forest/payment/5f7d6edc5f2ec6015f124cf2/relationships/schedules?fields%5Bpayment%5D=_id%2CorderId%2CcreatedAt%2CupdatedAt&fields%5BorderId%5D=orderNumber&page%5Bnumber%5D=1&page%5Bsize%5D=15&timezone=Europe%2FLondon&sort=-_id',
api_forest_1 | info: ┃ [ 6] path: '/forest/payment/5f7d6edc5f2ec6015f124cf2/relationships/schedules',
api_forest_1 | info: ┃ [ 7] query: {
api_forest_1 | info: ┃ [ 8] fields: { payment: '_id,orderId,createdAt,updatedAt', orderId: 'orderNumber' },
api_forest_1 | info: ┃ [ 9] page: { number: '1', size: '15' },
api_forest_1 | info: ┃ [10] timezone: 'Europe/London',
api_forest_1 | info: ┃ [11] sort: '-_id'
api_forest_1 | info: ┃ [12] },
api_forest_1 | info: ┃ [13] headers: {},
api_forest_1 | info: ┃ [14] source_ip: '172.18.0.1'
api_forest_1 | info: ┃ [15] },
api_forest_1 | info: ┃ [16] responseTime: 15,
api_forest_1 | info: ┃ [17] response: { status: 304, message: 'Not Modified', type: undefined, headers: {} }
api_forest_1 | info: ┃ [18] },
api_forest_1 | info: ┃ [19] service: 'api_forest',
api_forest_1 | info: ┃ [20] env: 'development',
api_forest_1 | info: ┃ [21] version: 'v1.4.9-72-g0e7e9371',
api_forest_1 | info: ┃ [22] context: {
api_forest_1 | info: ┃ [23] message: 'GET /forest/payment/5f7d6edc5f2ec6015f124cf2/relationships/schedules?fields%5Bpayment%5D=_id%2CorderId%2CcreatedAt%2CupdatedAt&fields%5BorderId%5D=orderNumber&page%5Bnumber%5D=1&page%5Bsize%5D=15&timezone=Europe%2FLondon&sort=-_id 304 15ms'
api_forest_1 | info: ┃ [24] },
api_forest_1 | info: ┃ [25] timestamp: '2020-10-08T10:00:49.351Z'
api_forest_1 | info: ┗ [26] }
Those requests happen after the schedule are sequalized. I don’t understand why Forest is doing this request:
ET /forest/payment/5f7d6edc5f2ec6015f124cf2/relationships/schedules?fields%5Bpayment%5D=_id%2CorderId%2CcreatedAt%2CupdatedAt&fields%5BorderId%5D=orderNumber&page%5Bnumber%5D=1&page%5Bsize%5D=15&timezone=Europe%2FLondon&sort=-_id