首页 > 解决方案 > 如何在 firebase 云函数中包含 js 函数和导入?

问题描述

我正在编写firebase云功能:

const {getContactObject} = require('../../../../src/modules/Contacts/scenes/Contactlist/ContactsManager/functions/getContactObject')

const getApiResponsible = require('../../functions/getApiResponsible')

const createContact = async payload => {
  console.log('payload', payload)
  console.log(getContactObject(getApiResponsible()))
}

module.exports = createContact

我的函数名为getContactObject位于项目的 src 文件夹中,它使用 es6 导入/导出

getContactObject.js

import { getCurDateWithUser } from '../../../../../../utilities/date/getCurDateWithUser'

export const getContactObject = uid => {
  return {
    lastName: '',
    name: '',
    middleName: '',
    gender: '',
    phone: [],
    email: [],
    messangers: [],
    social: [],
    address: [],
    dates: [],
    leads: [],
    sales: [],
    responsible: '',
    created: getCurDateWithUser(uid),
    updated: getCurDateWithUser(uid),
  }
}

我如何在使用节点 js 8 的 firebase 云功能中使用它?

是否可以在不重写的情况下导入getContactObject函数?

现在我遇到了关于导入的错误:

错误日志

标签: javascriptnode.jsfirebasegoogle-cloud-functions

解决方案


你有两个选择:

  1. 重写以下行:
import { getCurDateWithUser } from '../../../../../../utilities/date/getCurDateWithUser'

const getCurDateWithUser = require('../../../../../../utilities/date/getCurDateWithUser')
  1. 使用打字稿。在你tsconfig.json确保设置
{
  //...
  "compilerOptions": {
        //..
        "module": "commonjs"
  }
} 

推荐阅读