我正在实现一项服务来将消息发布到 Google Cloud Pub/Sub 主题。
import { PubSub } from '@google-cloud/pubsub'
import type { NotificationData } from './models/notification'
class NotificationService {
private pubsubClient: PubSub
private topicName = 'notifications'
constructor() {
this.pubsubClient = new PubSub()
}
async publishMessage(messageData: NotificationData) {
const data = JSON.stringify(messageData)
const dataBuffer = Buffer.from(data)
try {
const messageId = await this.pubsubClient
.topic(this.topicName)
.publishMessage({ data: dataBuffer })
console.log(`Message published (id: ${messageId})`)
} catch (error) {
console.error('Error publishing message:', error)
}
}
}
export const notificationService = new NotificationService()
当我在前端(vue3)组件中导入并使用该服务时,应用程序无法加载,并出现以下错误:
Uncaught TypeError: Class extends value undefined is not a constructor or null
at node_modules/@google-cloud/paginator/build/src/resource-stream.js (@google-cloud_pubsub.js?v=ccdc38b3:46169:49)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/paginator/build/src/index.js (@google-cloud_pubsub.js?v=ccdc38b3:46236:29)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/pubsub/build/src/pubsub.js (@google-cloud_pubsub.js?v=ccdc38b3:63297:23)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/pubsub/build/src/index.js (@google-cloud_pubsub.js?v=ccdc38b3:85020:20)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at @google-cloud_pubsub.js?v=ccdc38b3:85077:16@google-cloud_pubsub.js?v=ccdc38b3:46169
这发生在使用该服务的组件被渲染之前。我在图书馆问题或 stackoverflow 上的其他地方都找不到遇到相同问题的人。
你能弄清楚这一点吗?我遇到了同样的问题,任何帮助将不胜感激