"TypeError: Cannot read property 'startsWith' of undefined"

I have a funtion node with many functions in it. On one function I get the following error message: "TypeError: Cannot read property 'startsWith' of undefined"

What causes this message. Any help appreciated

it means you are calling startsWith(...) on a variable that is undefined somewhere in your function.

use node.warn(my_var) in your function to check a value of a variable before you call startsWith

I am not calling startsWith(...) ....
here my function code:

function runtime_stop(){
    //let end = Date.now();
    let runtime = Date.now() - context.get(starttime);
    msg.topic = "runtime";
    msg.payload = runtime;

    msg.topic = "runtimestr";
    msg.payload = runtime_str(runtime);
    let total = context.get(runtimetotal) + runtime;
    context.set("runtimetotal", total);
    msg.topic = "runtimetotal";
    msg.payload = total;

It could be coming from a sub function like context.get

Put a node.warn call above the context.get and check what value is in starttime and runtimetotal

PS, I assume that's not the full code since there is no call to your function. Have you checked all of the function code?

problem solved.
I forgot the " in context.get :frowning:

many thanks for your help.
node warn helped!

So it looks like passing in undefined as the first argument to context.get causes that error. We ought to handle that case and either return undefined or throw a more descriptive error.

If anyone fancies a PR'ing a fix, it'll be around here: node-red/index.js at master · node-red/node-red · GitHub

I dont mind pickling that up Nick. Against master or dev?

PS: this should become less of a pain point when users (hopefully) switch to using monaco in v2.0.0 :slight_smile:)

master please - and have a look at set as well just in case :slight_smile:


I'm also with that error...nothing is working for me. What could it be?

const Discord = require("discord.js")
const fs = require("fs")
var env = {}
fs.stat("./env.json", (err, stats) => { if (!err) env = require("./env.json") })

const firebase = require("firebase-admin")
const firebaseApp = firebase.initializeApp({
    credential: firebase.credential.cert((process.env.FIREBASECERT || JSON.stringify(env.FIREBASECERT)) .startsWith("{") ? JSON.parse(process.env.FIREBASECERT || JSON.stringify(env.FIREBASECERT)) : JSON.parse(fs.readFileSync(process.env.FIREBASECERT))),
    databaseURL: "https://kale-bot-discord-default-rtdb.firebaseio.com"


This issue was due to context. You problem has nothing to do with context (you arent using context)

If you are getting TypeError: Cannot read property ‘startsWith’ of undefined it will be because (process.env.FIREBASECERT || JSON.stringify(env.FIREBASECERT)) returns null, undefined or is NOT a string.

Please open a new thread if you still have this issue.