我在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
我没有在本地托管弹性搜索。我使用的是云版本。我使用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
}
我对web/后端东西很陌生,所以我不确定发生了什么。我到处都找过了。我看到很多人提到这个错误,但是他们要么使用不同的堆栈,要么在本地托管elasticsearch,所以他们的解决方案对我不起作用。这个错误似乎表明elasticsearch正在试图与http://localhost:9200/
建立连接。我不确定为什么会发生这种情况,因为我正在使用我的云id和凭据。如有任何帮助,不胜感激。
您似乎正在使用非常旧版本的elasticsearch
客户端库。现在您需要使用新版本,并像这样安装它
npm install @elastic/elasticsearch
然后你的代码就可以工作了。