https://pulumi.com logo
#general
Title
# general
q

quaint-queen-45003

07/01/2019, 5:44 PM
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

lemon-spoon-91807

07/01/2019, 6:23 PM
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

quaint-queen-45003

07/01/2019, 6:40 PM
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

lemon-spoon-91807

07/01/2019, 6:45 PM
Ok 🙂
q

quaint-queen-45003

07/01/2019, 6:57 PM
or is there any easy why to use an array of values from yaml with pulumi config files?
l

lemon-spoon-91807

07/01/2019, 6:57 PM
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

quaint-queen-45003

07/01/2019, 6:58 PM
Copy code
Property 'getObject' does not exist on type 'string'.ts
when i set the json encoded string as default within config.ts
l

lemon-spoon-91807

07/01/2019, 6:58 PM
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

quaint-queen-45003

07/01/2019, 6:59 PM
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

lemon-spoon-91807

07/01/2019, 7:00 PM
sounds good!
q

quaint-queen-45003

07/01/2019, 7:06 PM
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

lemon-spoon-91807

07/01/2019, 7:07 PM
ok. so that's just exporting a string.
q

quaint-queen-45003

07/01/2019, 7:07 PM
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

lemon-spoon-91807

07/01/2019, 7:08 PM
right. you're importing a string, and callin .getObject on it 🙂
q

quaint-queen-45003

07/01/2019, 7:08 PM
and the error is
Copy code
'getObject' does not exist on type 'string'.ts
l

lemon-spoon-91807

07/01/2019, 7:08 PM
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

quaint-queen-45003

07/01/2019, 7:12 PM
ya it makes sense. i guess i'm confused why
getObject
works when pulling same string from config files
l

lemon-spoon-91807

07/01/2019, 7:13 PM
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

quaint-queen-45003

07/01/2019, 7:13 PM
bah i see
thanks 🙂
l

lemon-spoon-91807

07/01/2019, 7:13 PM
in this case,
.get
(or
.getObject
) is being called on a Config object, not a string
great! 🙂
q

quaint-queen-45003

07/01/2019, 7:25 PM
Thanks for the help. Im new to JS and it still bites me sometimes 🙂
l

lemon-spoon-91807

07/01/2019, 7:26 PM
absolutely! 🙂
i recommend TypeScript 😄
it helps a lot for these types of things