当尝试通过Firestore云函数在Nodejs中使用ElasticSearch时,得到“无活动连接”

yc0p9oo0  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(148)

我在firestore上有一个云函数,看起来像这样:

const firebaseFunctions = functions.region("australia-southeast1").firestore;

const {Client} = require("elasticsearch");
const client = new Client({
  cloud: {
    id: env.elasticsearch.id,
  },
  auth: {
    username: env.elasticsearch.username,
    password: env.elasticsearch.password,
  },
});

exports.onBookingCreated = firebaseFunctions
    .document("bookings/{bookingId}")
    .onCreate(async (snap, context) => {
      const bookingData = snap.data();
      try {
        const response = await client.index({
          index: "all_bookings",
          id: context.params.bookingId,
          document: bookingData,
        });
        return response;
      } catch (err) {
        console.log(err.toString());
      }
    });

上面的函数被触发,但索引失败。我得到以下日志:

PS C:\Users\...\functions> firebase functions:log --only onBookingCreated
2022-11-09T00:39:42.120814Z ? onBookingCreated:       at HttpConnector.<anonymous> (/workspace/node_modules/elasticsearch/src/lib/connectors/http.js:171:7)
2022-11-09T00:39:42.120820Z ? onBookingCreated:       at ClientRequest.wrapper (/workspace/node_modules/lodash/lodash.js:4991:19)
2022-11-09T00:39:42.120825Z ? onBookingCreated:       at ClientRequest.emit (node:events:513:28)
2022-11-09T00:39:42.120844Z ? onBookingCreated:       at ClientRequest.emit (node:domain:489:12)
2022-11-09T02:43:38.852929Z ? onBookingCreated:       at Log.error (/workspace/node_modules/elasticsearch/src/lib/log.js:239:56)
2022-11-09T02:43:38.852934Z ? onBookingCreated:       at checkRespForFailure (/workspace/node_modules/elasticsearch/src/lib/transport.js:298:18)
2022-11-09T02:43:38.852940Z ? onBookingCreated:       at HttpConnector.<anonymous> (/workspace/node_modules/elasticsearch/src/lib/connectors/http.js:171:7)
2022-11-09T02:43:38.852945Z ? onBookingCreated:       at ClientRequest.wrapper (/workspace/node_modules/lodash/lodash.js:4991:19)
2022-11-09T02:43:38.852951Z ? onBookingCreated:       at ClientRequest.emit (node:events:513:28)
2022-11-09T02:43:38.852955Z ? onBookingCreated:       at ClientRequest.emit (node:domain:489:12)
2022-11-09T02:43:38.852960Z ? onBookingCreated:       at Socket.socketErrorListener (node:_http_client:481:9)
2022-11-09T02:43:38.852967Z ? onBookingCreated:       at Socket.emit (node:events:513:28)
2022-11-09T02:43:38.852972Z ? onBookingCreated:       at Socket.emit (node:domain:489:12)
2022-11-09T02:43:38.852977Z ? onBookingCreated:       at emitErrorNT (node:internal/streams/destroy:157:8)
2022-11-09T02:43:38.855848Z ? onBookingCreated: Elasticsearch WARNING: 2022-11-09T02:43:38Z
2022-11-09T02:43:38.855861Z ? onBookingCreated:   Unable to revive connection: http://localhost:9200/
2022-11-09T02:43:38.856087Z ? onBookingCreated: Elasticsearch WARNING: 2022-11-09T02:43:38Z
2022-11-09T02:43:38.856094Z ? onBookingCreated:   No living connections
2022-11-09T02:43:38.860774341Z D onBookingCreated: Function execution took 908 ms. Finished with status: ok
2022-11-09T02:43:39.900329Z ? onBookingCreated: Error: No Living connections

我没有在本地托管ElasticSearch。我使用的是云版本。我使用npm install elasticsearch安装ElasticSearch。我的package.json看起来像这样:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "axiom": "^0.1.6",
    "axios": "^1.1.3",
    "elasticsearch": "^16.7.3",
    "express": "^4.18.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "request": "^2.88.2",
    "request-promise": "^4.2.6",
    "stripe": "^10.12.0-beta.1"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

我对网络/后端的东西还很陌生,所以我不太清楚到底发生了什么。我到处都搜索过。我看到很多人提到这个错误,但是他们要么使用了不同的堆栈,要么他们在本地托管了elasticsearch,所以他们的解决方案对我不起作用。这个错误似乎表明elasticsearch正在试图与http://localhost:9200/建立连接。我不知道为什么会这样。s发生,因为我正在使用我的云id和凭据。任何帮助都非常感谢。

hzbexzde

hzbexzde1#

您似乎正在使用elasticsearch客户端库的very old version

npm install @elastic/elasticsearch

然后你的代码就可以工作了。

相关问题