phonemyatt
phonemyatt

Reputation: 1377

functions: Error from emulator. Error occurred while parsing your function triggers

I'm testing firebase functions locally in my existing project in typescript. When i run following command, i always get typeerror even though i can upload it to firebase project. the error always occured after typescript compile to js

firebase serve --only functions

TypeError: Cannot read property 'username' of undefined at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\email\index.js:8:52) at Module._compile (module.js:635:30) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Module.require (module.js:579:17) at require (internal/module.js:11:18) at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\index.js:19:15) at Module._compile (module.js:635:30)

import * as functions from 'firebase-functions';
import * as nodemailer from 'nodemailer';
const accountname = functions.config().emailaccount.username;
const accountpassword = functions.config().emailaccount.password;
/* smtp configuration */

I already set emailaccount in my project, below is my code.

firebase functions:config:set emailaccount.username="[email protected]" 
emailaccount.password="mypassword"

Please note that it is working fine when uploaded to firebase functions but it's not when serving locally. what can i do to make it work locally?

Finally, I just replace all my credentials with actual values instead of getting from firebase config. And I cd to functions folder and type 'npm start'. it look like when i run 'firebase serve --only functions', it doesn't apply with recent changes so give me the same error until I type 'npm start' which will run tsc and firebase shell, stop it (ctrl+c) then type 'firebase serve --only functions' again. Fyi, I created my current firebase functions project using firebase cli and typescript.

firebase cli - 3.18.4
firebase functions - ^1.0.1
firebase admin - 5.12.0
@google-cloud/storage - 1.6

Upvotes: 0

Views: 579

Answers (2)

phonemyatt
phonemyatt

Reputation: 1377

After testing for long period of time, I decided to put username, password and tokens directly in my functions for local testing although I don't really like this idea and I change back to functions.config().. when i deploy to firebase. Thanks for @Frank for answers

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

There is no built-in config parameter called emailaccount, so it seems you defined it yourself.

The local environment does not automatically contain these variables. You will actually have to define them in a file called runtimeconfig.json. An easy way to do that is shown here:

cd functions
firebase functions:config:get > .runtimeconfig.json

Upvotes: 0

Related Questions