how can I get the string of RandomPassword, I trie...
# general
r
how can I get the string of RandomPassword, I tried almost 2 hours, but always get error:
Copy code
Calling [toJSON] on an [Output<T>] is not supported.
To get the value of an Output as a JSON value or JSON string consider either:
    1: o.apply(v => v.toJSON())
    2: o.apply(v => JSON.stringify(v))

See <https://pulumi.io/help/outputs> for more details.
This function may throw in a future version of @pulumi/pulumi
``````
Copy code
const dbpassword = new random.RandomPassword(`${userName}-password`, {
  length: 16,
  special: true,
  overrideSpecial: `_%@`,
});
const textMessage = dbpassword.result.apply(s => `{username: ${userName}, password: ${s}}`);
const passwordString = dbpassword.result.apply(v => `password: ${v}`);
const content = dbpassword.result.apply( (v) => `
  username: ${userName}
  password: "${v}"
`);
console.log(passwordString);

sendMessageToFeishu(webhookURL, {
  msg_type: "post",
  content: {
    post: {
      zh_cn: {
        title: "MetaDB Password",
        content: [[
          { tag: "text", text: content },
          // { tag: "text", text: `username: ${userName}  ;  ` },
          { tag: "text", text: passwordString },
        ]],
      },
    },
  },
});
that's my code
@gorgeous-country-43026 pls help me
g
Lol, so I'm the go-to guy to help now 😅 No worries, I'll help
new random.RandomPassword
returns an
Output<string>
. That's not the same as plain string
What that means is that it's a string value that will get realized sometime in the future
Output
has a method called
apply
which also returns an
Output
So that you can chain these together and keep the logic sound
Basically if you are calling functions which require you to pass a plain string to them and that string is being generated by an
Output
you need to do something like this:
Copy code
const someOutput = new random.RandomPassword(.....);
someOutput.apply(password => console.log('Password is:", password));
You have the "raw" value available within the handler function of
apply
. Pulumi will call that function after the
Output
has realized it's value
It's an async world
Hope that helps
Oh, one more thing. If you have several `Output`s and you need to get the raw value from all of those there's
pulumi.all
function to wait for all of them at the same time. Basically you'll do this:
Copy code
const dbPassword = new random.RandomPassword(...);
const apiPassword = new random.RandomPassword(...);

pulumi.all({
  db: dbPassword,
  apiPassword
}).apply({db, apiPassword} => {
  // This is called *only* after *both* Outputs are realized
  console.log("DB password:", db);
  console.log("API password:", apiPassword);
});
b
@rhythmic-actor-14991 please do not
@
individual people asking for help there's a blog post here explaining why you can't get this value outside of an apply: https://www.leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html Thanks for helping out @gorgeous-country-43026 - much appreciated
r
I use the
apply
, but still get no effect..
Copy code
{ tag: "text", text:  dbpassword.result.apply(result => `Password is:, ${result}`) },
a
@rhythmic-actor-14991 when they say in the apply, they mean in the code block....so you want something like
Copy code
const someOutput = new random.RandomPassword(.....);

// out here someOutput is an output and you can't get the value

someOutput.apply( => {
 // in here someOutput is a string

 console.log('Password is:", someOutput);
 // you can use the value like this 
 sendMessageToFeishu(webhookURL, {
  msg_type: "post",
  content: {
    post: {
      zh_cn: {
        title: "MetaDB Password",
        content: [[
          { tag: "text", text: content },
          { tag: "text", text: someOutput },
        ]],
      },
    },
  },
});
  
});
r
@Patrick Flaherty that's great... sorry for such stupid question,... I should learn pulumi deeply..