我在 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. 我的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/。我不確定為什么會這樣,因為我正在使用我的云 ID 和憑據。任何幫助是極大的贊賞。
uj5u.com熱心網友回復:
您似乎使用的是非常舊版本的elasticsearch客戶端庫。您現在需要使用新的并像這樣安裝
npm install @elastic/elasticsearch
然后你的代碼就會作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533382.html
