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

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212>\
**Category:** General\
**Created:** [25 May 2021 11:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212 "2021-05-25T11:52:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 May 2021 11:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/1 "2021-05-25T11:52:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2021 11:57 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/2 "2021-05-25T11:57:46Z")

</div>

> [@juntiedt](#):
>
> What causes this message

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`

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 May 2021 12:16 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/3 "2021-05-25T12:16:48Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2021 12:19 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/4 "2021-05-25T12:19:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 May 2021 12:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/5 "2021-05-25T12:41:22Z")

</div>

> [@Steve-Mcl](#):
>
> node.warn(my\_var)

problem solved.  
I forgot the " in context.get ☹

many thanks for your help.  
node warn helped!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 May 2021 12:45 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/6 "2021-05-25T12:45:24Z")

</div>

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](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/runtime/lib/nodes/context/index.js#L256)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2021 12:50 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/7 "2021-05-25T12:50:48Z")

</div>

> [@knolleary](#):
>
> If anyone fancies a PR'ing a fix, it'll be around here

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce7442ea4171ba8929d03639247e17c24f2e8371.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 May 2021 12:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/8 "2021-05-25T12:52:13Z")

</div>

> [@Steve-Mcl](#):
>
> master or dev

master please - and have a look at `set` as well just in case 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2021 14:06 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/9 "2021-05-25T14:06:48Z")

</div>

done.

> <https://github.com/node-red/node-red/pull/2993>
>
> \- \[x\] Bugfix (non-breaking change which fixes an issue)
> \- \[\] New feature (non-…breaking change which adds functionality)
> 
> 
> \## Proposed changes
> 
> As discussed \[here\](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/8?u=steve-mcl)
> 
> Ensure \`key\` is a string when getting and setting context. 
> 
> At first i used the error string \`"key must be a string"\` 
> !\[image\](https://user-images.githubusercontent.com/44235289/119511667-8cb83e80-bd6a-11eb-8a67-e76b402f78bd.png)
> 
> ... but once i tested it, i felt it was net clear enough to the user where this error was coming from. 
> 
> I eventually settled on \`"context set() requires 'key' to be a valid string"\` and \`"context set() requires 'key' to be a valid string"\`
> 
> Any thoughts on a better error string?
> 
> \<br\>
> 
> This is the result of my test...
> !\[image\](https://user-images.githubusercontent.com/44235289/119511516-672b3500-bd6a-11eb-87db-86b25a45efab.png)
> 
> \`\`\`
> \[{"id":"95d47f9e.765ca","type":"function","z":"c559743c.82b088","name":"context.set(undefined, 1) ERROR","func":"context.set(undefined, 1);\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1580,"y":200,"wires":\[\[\]\]},{"id":"9f4dc13d.09dc9","type":"function","z":"c559743c.82b088","name":"context.set(null, 1) ERROR","func":"context.set(null, 1)\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1560,"y":240,"wires":\[\[\]\]},{"id":"7ce6aac7.0dd234","type":"function","z":"c559743c.82b088","name":"context.set({}, 1) ERROR","func":"context.set({}, 1)\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1550,"y":280,"wires":\[\[\]\]},{"id":"ffd4b116.01345","type":"inject","z":"c559743c.82b088","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1290,"y":349,"wires":\[\["95d47f9e.765ca","9f4dc13d.09dc9","7ce6aac7.0dd234","bf7f54b.42e57a8","d96b1924.cd1838","22ad3e8e.bd5ed2","d22e1d25.fc103","9e582c35.8c57e","f6ba35f9.e9cee8","b67317bd.6c0158","507cc5a0.6e0bdc","43f7ed8a.8935e4"\]\]},{"id":"bf7f54b.42e57a8","type":"function","z":"c559743c.82b088","name":"context.set(1, 1) ERROR","func":"context.set(1, 1)\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1550,"y":160,"wires":\[\[\]\]},{"id":"d96b1924.cd1838","type":"function","z":"c559743c.82b088","name":"context.set(\\"1\\", 1) OK","func":"context.set(\\"1\\", 1);\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1540,"y":120,"wires":\[\[\]\]},{"id":"9e582c35.8c57e","type":"function","z":"c559743c.82b088","name":"context.get(undefined) ERROR","func":"context.get(undefined);\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1570,"y":460,"wires":\[\[\]\]},{"id":"f6ba35f9.e9cee8","type":"function","z":"c559743c.82b088","name":"context.get(null) ERROR","func":"context.get(null)\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1550,"y":500,"wires":\[\[\]\]},{"id":"b67317bd.6c0158","type":"function","z":"c559743c.82b088","name":"context.get({}) ERROR","func":"context.get({})\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1540,"y":540,"wires":\[\[\]\]},{"id":"d22e1d25.fc103","type":"function","z":"c559743c.82b088","name":"context.get(1) ERROR","func":"context.get(1)\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1540,"y":420,"wires":\[\[\]\]},{"id":"22ad3e8e.bd5ed2","type":"function","z":"c559743c.82b088","name":"context.get(\\"1\\", 1) OK","func":"context.get(\\"1\\");\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1540,"y":380,"wires":\[\[\]\]},{"id":"507cc5a0.6e0bdc","type":"function","z":"c559743c.82b088","name":"context.set(\\"\\", 1) ERROR","func":"context.set(\\"\\", 1);\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1550,"y":320,"wires":\[\[\]\]},{"id":"43f7ed8a.8935e4","type":"function","z":"c559743c.82b088","name":"context.get(\\"\\") ERROR","func":"context.get(\\"\\")\\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":\[\],"x":1540,"y":580,"wires":\[\[\]\]}\]
> \`\`\`
> 
> 
> 
> \## Checklist
> \- \[x\] I have read the \[contribution guidelines\](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
> \- \[x\] For non-bugfix PRs, I have discussed this change on the forum/slack team.
> \- \[\] I have run \`grunt\` to verify the unit tests pass
> \- \[\] I have added suitable unit tests to cover the new/changed functionality

---

<div class="post-metadata">

**Author:** ![DiadDenis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diaddenis/32/45433_2.png) [@DiadDenis](https://discourse.nodered.org/u/DiadDenis)\
**Post date:** [24 July 2021 00:12 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/10 "2021-07-24T00:12:32Z")

</div>

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

```auto
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"
})

```

.startsWith("{")

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 July 2021 07:57 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/11 "2021-07-24T07:57:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 July 2021 07:57 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212/12 "2021-07-24T07:57:34Z")

</div>


