# Custom endpoint does not save records to index

**URL:** <https://discuss.elastic.co/t/custom-endpoint-does-not-save-records-to-index/184153>\
**Category:** Kibana\
**Created:** [June 4, 2019, 10:17am UTC](https://discuss.elastic.co/t/custom-endpoint-does-not-save-records-to-index/184153 "2019-06-04T10:17:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![masterThomas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/masterthomas/32/42765_2.png) [@masterThomas](https://discuss.elastic.co/u/masterThomas)\
**Post date:** [June 4, 2019, 10:17am UTC](https://discuss.elastic.co/t/custom-endpoint-does-not-save-records-to-index/184153/1 "2019-06-04T10:17:32Z")

</div>

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:*'
    });
  },
});
```

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [June 4, 2019, 6:26pm UTC](https://discuss.elastic.co/t/custom-endpoint-does-not-save-records-to-index/184153/2 "2019-06-04T18:26:03Z")

</div>

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

```auto
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);
  },
});

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 2, 2019, 6:26pm UTC](https://discuss.elastic.co/t/custom-endpoint-does-not-save-records-to-index/184153/3 "2019-07-02T18:26:08Z")

</div>

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