hello - i'm having some troubles with `getObject` ...
# general
q
hello - i'm having some troubles with
getObject
and JSON encoded strings. The error is
Property 'getObject' does not exist on type 'string'.ts(2339)
. Any suggestions on this? I'll leave my code sample in a thread
Copy code
export let testConfig = config.get("testConfig") || "\"{testConfig\": \"test\"}";
let getConfig = testConfig.getObject(testConfig);
l
so, question: i'm unfamiliar with 'getObject' (and i can't find any docs on it).
What are you expecting to have happen there?
however, you called .getObject on a string.
what you want to do is
config.getObject
not
testConfig.getObject
.
Note: typescript will def help you out here 🙂
q
hmm so it actually gets a bit more complicated than that
so in my
config.ts
i've got
Copy code
let newrelicConfig = new pulumi.Config("newrelic");
export let newrelicAlertChannels = newrelicConfig.get("alertChannels") || "{\"alertChannels\":[{\"name\":\"slack\"}, {\"name\":\"pagerduty\"}}
im then importing
config.ts
into a new file
where i'm trying to do this
Copy code
import * as config from "../config";
import { AlertChannelArgs } from "@pulumi/newrelic";

export interface NewRelicAlertChannel {
    name: string,
    type: string,
    configuration: AlertChannelArgs,
}
export const newrelicAlertChannels = config.newrelicConfig.getObject<Array<NewRelicAlertChannel>>("alertChannels");
l
Ok 🙂
q
or is there any easy why to use an array of values from yaml with pulumi config files?
l
so all .getObject does is read out the string value, and then call JSON.parse on it
what sort of error are you getting with the above?
q
Copy code
Property 'getObject' does not exist on type 'string'.ts
when i set the json encoded string as default within config.ts
l
can you link me to your code?
it looks like you're simply calling .getObject on a string, not on hte real config object.
but it's hard to tell given what i can see so far.
q
sec im trying to piece together a code sample while in a meeting. i'll grab my code shortly when i get back to my desk
l
sounds good!
q
so here's what i have based on previous suggestions
in
config.ts
Copy code
export let newrelicAlertChannels =  newrelicConfig.get("alertChannels") || "\"{alertChannels\": [{\"name\":\"slack\"}, {\"name\":\"pagerduty\"}]}";
l
ok. so that's just exporting a string.
q
yup then in a different file
Copy code
import { newrelicAlertChannels } from "../config";

export interface NewRelicAlertChannel {
    name: string,
    type: string,
    configuration: AlertChannelArgs,
}
 
export const alertingChannels= newrelicAlertChannels.getObject<Array<NewRelicAlertChannel>>("alertChannels");
l
right. you're importing a string, and callin .getObject on it 🙂
q
and the error is
Copy code
'getObject' does not exist on type 'string'.ts
l
one way you could address this (not necessarily teh best way)is to simply do this:
export const alertingChannels = JSON.parse(newrelicAlertChannels)
note: the issue you're runnign into above is not really a pulumi issue 🙂 it's something that would't work in JS at all. it's equivalent to:
Copy code
file1.js
export let foo = "[1, 2, 3]";

file2.js
import foo from "file1";
let bar = foo.getObject("x");
does that make sense? 🙂
q
ya it makes sense. i guess i'm confused why
getObject
works when pulling same string from config files
l
because you're calling it on a
pulumi.Config
object
i.e.
Copy code
let newrelicConfig = new pulumi.Config("newrelic");
export let newrelicAlertChannels = newrelicConfig.get("alertChannels")
q
bah i see
thanks 🙂
l
in this case,
.get
(or
.getObject
) is being called on a Config object, not a string
great! 🙂
q
Thanks for the help. Im new to JS and it still bites me sometimes 🙂
l
absolutely! 🙂
i recommend TypeScript 😄
it helps a lot for these types of things