我正在尝试在Cloud Firestore中设置一个功能,该功能将在将新文档添加到特定集合时触发。我希望使用TypeScript做到这一点,因为在编写异步代码时,我被告知它更易于使用。以下JavaScript代码会按预期运行,并在我在指定位置添加新文档时触发:
//This JavaScript code works
import * as functions from 'firebase-functions';
exports.waitReportCreatedJS = functions.firestore
.document('merchant_locations/{merchantId}/wait_period/{waitId}')
.onCreate((snapshot, context) => {
console.log('WaitReportCreated has been triggered');
const merchantId = context.params.merchantId
const waitId = context.params.waitId
console.log(`New wait recorded: ${waitId} at ${merchantId}`);
return snapshot.ref.update({ waitId: waitId })
});
但是,当我尝试使用TypeScript执行相同操作时,它无法触发-添加文档时什么也没发生:
//But this TypeScript code doesn't seem to even trigger
import * as functions from 'firebase-functions';
export const waitReportCreatedTS = functions.database
.ref('merchant_locations/{merchantId}/wait_period/{waitId}')
.onCreate((snapshot, context) => {
console.log('WaitReportCreated has been triggered');
const merchantId = context.params.merchantId
const waitId = context.params.waitId
console.log(`New wait recorded: ${waitId} at ${merchantId}`);
return snapshot.ref.update({ waitId: waitId })
})
我对Firestore功能非常陌生,完全不知道我做错了什么。谢谢您能给我的帮助。
答案 0 :(得分:2)
在您的JavaScript示例中,您使用了Firestore数据库functions.firestore
.document
。但是,您的TypeScript示例使用的是实时数据库functions.database.ref
。
import * as functions from 'firebase-functions';
export const waitReportCreatedTS = functions.firestore
.document('merchant_locations/{merchantId}/wait_period/{waitId}')
.onCreate((snapshot, context) => {
console.log('WaitReportCreated has been triggered');
const merchantId = context.params.merchantId
const waitId = context.params.waitId
console.log(`New wait recorded: ${waitId} at ${merchantId}`);
return snapshot.ref.update({ waitId: waitId })