I have the following code for POSTing a record via axios to my DB, and then I would like to search for said record by its ID match as well as list all the latest records.
I have confirmed, via checking Dynamo, that the record gets added to the DB but cannot get it to appear when I look for it via the GET /search.
It makes me wonder if I’m somehow making a mistake.
I know that my database has 6000+ records of this same type, so I could just be getting unlucky (as I have not yet included pagination in my queries)… but I’ve tested this about 50 times with 0 retrieval success via the query vs running the same POST and GET via Postman and having about a 50% success rate.
test('post result then search for presence', async () => {
const trafficAnalyticMessage = buildTrafficAnalyticMessage();
const result = await axios.post(messagesUrl, trafficAnalyticMessage, {
headers: {
'x-api-key': 'redacted',
},
});
const message = result.data as dataMessageApi.Message;
logger.log(`Message ${util.inspect(message)}`);
// logger.log(`Result ${util.inspect(result)}`);
const results = await axios.get(searchUrl, {
headers: {
'x-api-key': 'redacted',
},
params: {
type: 'analytic',
subtype: 'traffic_volumes',
startTime: 1,
},
});
logger.log(
'Attempting to search for message with id: ' + JSON.stringify(message.id),
);
const messages = results.data as dataMessageApi.Message[];
const found = messages.find((m) => m.id === message.id);
logger.log('Message returned from search: ' + JSON.stringify(found));
// expect(found).toBeDefined();
let date: string;
// log length, first and last msg id
logger.log(`Length of search response: ${messages.length}`);
// 1
logger.log('First ID: ' + JSON.stringify(messages[0].id));
date = new Date(messages[0].timestamp!).toUTCString();
logger.log(`First timestamp: ${date}`);
// 2
logger.log('Second ID: ' + JSON.stringify(messages[1].id));
date = new Date(messages[1].timestamp!).toUTCString();
logger.log(`Second timestamp: ${date}`);
// 3
logger.log('Third ID: ' + JSON.stringify(messages[2].id));
date = new Date(messages[2].timestamp!).toUTCString();
logger.log(`Third timestamp: ${date}`);
// 4
logger.log('Fourth ID: ' + JSON.stringify(messages[3].id));
date = new Date(messages[3].timestamp!).toUTCString();
logger.log(`Fourth timestamp: ${date}`);
// Fifth from last
logger.log(
'Fifth from last ID: ' +
JSON.stringify(messages[messages.length - 5].id),
);
date = new Date(messages[messages.length - 5].timestamp!).toUTCString();
logger.log(`Fifth to last timestamp: ${date}`);
logger.log(
'Fourth from last ID: ' +
JSON.stringify(messages[messages.length - 4].id),
);
date = new Date(messages[messages.length - 4].timestamp!).toUTCString();
logger.log(`Fourth from last timestamp: ${date}`);
logger.log(
'Third from last ID: ' +
JSON.stringify(messages[messages.length - 3].id),
);
date = new Date(messages[messages.length - 3].timestamp!).toUTCString();
logger.log(`Third from last timestamp: ${date}`);
logger.log(
'Second to last ID: ' + JSON.stringify(messages[messages.length - 2].id),
);
date = new Date(messages[messages.length - 2].timestamp!).toUTCString();
logger.log(`Second to last timestamp: ${date}`);
logger.log('Last ID: ' + JSON.stringify(messages[messages.length - 1].id));
date = new Date(messages[messages.length - 1].timestamp!).toUTCString();
logger.log(`Last timestamp: ${date}`);
expect(result).toBeDefined();
expect(result.status).toBe(201);
});
This is the output I am getting:
Any help would be greatly appreciated.
**Also, please can you advise me how to add the GET as a .then result of the POST? Or a promise or some kind of 2-part resolution? I have had great trouble figuring out how that works. I’m new to this kind of work.
Thanks so much!!**