SyntaxError:不能在模块 Firebase 函数之外使用 import 语句

Chr*_*ans 7 javascript firebase google-cloud-functions

我的 Firebase 功能有问题。我收到以下错误。

语法错误:不能在模块外使用导入语句

下面是我的代码:

import * as functions from 'firebase-functions';
import * as sgMail from '@sendgrid/mail';

sgMail.setApiKey(key);

export const weeklyReminder = functions.pubsub.schedule('every Wednesday 21:00').onRun(async context =>{

    const msg = {
        to: 'email@gmail.com',
        ...
    };
    return sgMail.send(msg);

}); 
Run Code Online (Sandbox Code Playgroud)

如何导入 firebase 函数?

Jas*_*rne 9

您使用的是 TypeScript 还是 vanilla JavaScript?使用纯 JavaScript,您可以像这样使用 require:

const functions = require('firebase-functions');
Run Code Online (Sandbox Code Playgroud)

此外,将函数更改为与以下相同:

exports.weeklyReminder = functions.pubsub.schedule('every Thursday 21:00').onRun(
Run Code Online (Sandbox Code Playgroud)