Unable to connect elastic search with node.js client

I'm trying to use elastic search client for node.js

I was able to get a response with my curl command

However, my client for node.js it didn't print out any result or error message.

my working Curl is shown as follow

curl -X GET -H 'Content-Type:application/json' -u 'username:password' https://venus.example.com/my_example_index/_search -d '
{"query":{"bool":{"must":[{"term":{"main.message.neworder":true}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}}'i

some part of my node.js codes for the elastic connection is shown as follow

function getElastic() {

const { Client } = require('@elastic/elasticsearch');
const { URL } = require('url');

    const client = new Client({
       
        node: {
            url: new URL('https://venus.example.com'),
            username: 'username',
            password: 'password',
        },
    });

  let body = async () => {
        try {
            return await client.search({
                index: 'my_example_index',
                body: {
                    query: {
                        bool: {
                            must: [{ term: { 'main.message.neworder': true } }],
                            must_not: [],
                            should: [],
                        },
                    },
                },
            });
        } catch (error) {
            console.error(error);
        }
    };
}

please give me some useful advice to get a successful result.

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