# Npm ES library - how to do a upsert?

**URL:** <https://discuss.elastic.co/t/npm-es-library-how-to-do-a-upsert/140018>\
**Category:** Elasticsearch\
**Created:** [July 14, 2018, 2:55am UTC](https://discuss.elastic.co/t/npm-es-library-how-to-do-a-upsert/140018 "2018-07-14T02:55:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darin\_Hensley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darin_hensley/32/32794_2.png) [@Darin\_Hensley](https://discuss.elastic.co/u/Darin_Hensley)\
**Post date:** [July 14, 2018, 2:55am UTC](https://discuss.elastic.co/t/npm-es-library-how-to-do-a-upsert/140018/1 "2018-07-14T02:55:50Z")

</div>

Using the ES node library.... how do I do a upsert? After reading documentation, I thought this would work but not having success. My goal is to insert a new document, if it already exists, then update it(or do nothing if there were no changes).

I get `TypeError: Unable to build a path with those params. Supply at least index, type, id`

from:

```
 const upsertArtist = (artist) => {
  var doc = {
    scripted_upsert: true,
    script: {
      index: 'song',
      type: '_doc',
      body: {
        type: 'artist',
        artist: artist
      },
    },

    upsert: {},

  };

  return client.update(doc);
};
```

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [July 16, 2018, 5:45am UTC](https://discuss.elastic.co/t/npm-es-library-how-to-do-a-upsert/140018/2 "2018-07-16T05:45:39Z")

</div>

The content of the `doc.script` needs to go into a nested `body` attribute. Check the [docs](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update) for the proper syntax.

---

<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:** [August 13, 2018, 5:45am UTC](https://discuss.elastic.co/t/npm-es-library-how-to-do-a-upsert/140018/3 "2018-08-13T05:45:40Z")

</div>

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