【问题标题】:SyntaxError: Cannot use import statement outside a module Firebase FunctionsSyntaxError:无法在模块 Firebase 函数之外使用 import 语句
【发布时间】:2020-05-02 20:03:07
【问题描述】:

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

SyntaxError: 不能在模块外使用 import 语句

下面是我的代码:

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);

}); 

如何导入 firebase 函数?

【问题讨论】:

    标签: javascript firebase google-cloud-functions


    【解决方案1】:

    您使用的是 TypeScript 还是原生 JavaScript?使用纯 JavaScript,你会像这样使用 require:

    const functions = require('firebase-functions');
    

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

    exports.weeklyReminder = functions.pubsub.schedule('every Thursday 21:00').onRun(
    

    【讨论】:

    • 如果你以这种方式导入它,你仍然可以使用“export const weekReminder”,如果不是,你是否导出函数
    • 也使用原生 JavaScript
    • 不你会使用:exports.weeklyReminder = functions.pubsub.schedule('every Wednesday 21:00').onRun(async context =>{
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2020-08-25
    • 2022-11-02
    • 2022-06-13
    • 2023-01-31
    • 2021-08-24
    相关资源
    最近更新 更多