首页 > 解决方案 > 如何在 Typescript 文件中调用外部 Javascript Firestore 函数?

问题描述

我正在使用 Node.js 上传带有云功能的 Typescript 文件( index.ts )。我现在有一个用外部 JavaScript 文件 ( notifyNewMessage.js ) 编写的 Firestore 触发器,我想以某种方式在 Typescript 文件中声明它。

这两个文件都在同一个目录中:

在此处输入图像描述


索引.ts:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

//declare javascript file here???

export const cloudFunction= functions
  .firestore.document(`Users/{user_id}`).onWrite(async (change, _context) => {

   //Function code....

}


notifyNewMessage.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.notifyNewMessage = functions.firestore{
   //Cloud function I want to declare in index.ts
}

如果有人可以为我提供一种方法,或者指出我可以学习的方向,那就太好了,因为 Typescript 和 JavaScript 不是我的强项:)

提前致谢!

标签: javascripttypescriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


假设您正在尝试从 js 文件中导入导出的函数。这不是简单的事情之一吗:

import notifyNewMessage from "./notifyNewMessage";

或者

import { notifyNewMessage } from "./notifyNewMessage";

推荐阅读