Custom endpoint does not save records to index

hi team,
Why my custom endpoint does not record to index and does not throw any errors? Use postman and get 200 status.

server.route({
method: 'POST',
path: '/api/record',
handler(req, h) {
  const payload = req.payload;
  callWithRequest(req, 'bulk', { 
    index: 'myindex', 
    '_type': 'doc', 
    body: {
      payload       
    }
  }).then(response => {
      ({error: null, id: response._id, result: response.result});
    })
    .catch(error => {
      ({error: error});
    });
    console.log("written!");
    return callWithRequest(req, 'search',{
      'index':'myindex',
      'q':'_id:*'
    });
  },
});

You code is not waiting for the initial callWithRequest to finish before returning the result of the second call. Try this modification (disclaimer: untested :slightly_smiling_face:):

server.route({
  method: 'POST',
  path: '/api/record',
  async handler(req, h) {
    const payload = req.payload;
    await callWithRequest(req, 'bulk', {
      index: 'myindex',
      _type: 'doc',
      body: {
        payload,
      },
    })
      .then(response => {
        console.log('written!');
        console.log({ error: null, id: response._id, result: response.result });
      })
      .catch(error => {
        console.log({ error: error });
      });
    const response = await callWithRequest(req, 'search', {
      index: 'myindex',
      q: '_id:*',
    });

    return h.response(response);
  },
});

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.