I have a problem with req.body.thread_id = thread.id
, when I call it in, it’s undetermined
app.post('/api/assistant/start', async (req, res) => {
const thread = await client.beta.threads.create()
console.log(""thread_id": " + thread.id);
req.body.thread_id = thread.id
return res.json(""thread_id": " + thread.id)
});
app.post('/api/assistant/chat', async (req, res) => {
const data = req.body.thread_id
console.log(data)
return res.json("Finish")
})
Output:
undefined
I use node.js and express with body-parser
Can you suggest another way to pass a variable between functions?
I’ve tried different variations of save call and saves. Always the same output.
2
Answers
you are passing the variable through the body and this is wrong because the body is filled by the request so to solve it try thread.id here =>
req.thread_id = thread.id
The issue you are encountering simply means that your
body
has nothread_id
field.Since your
POST
operation :Is the one that creates the
thread
and sends itsid
back to the client. One way would be to call your endpoints from the client-side as in the following, to ensure that they are called in the correct sequence. And thatthread_id
is passed to thePOST /api/assistant/chat
endpoint:The other post should remain as it is in your router: