0

I am new to Node js. Below code I have written.It does not show any output.

const pdClient = require('node-pagerduty');
const pdApiKey = 'XXXXXXXXXXXXXX';
const pd = new pdClient(pdApiKey);
let from = 'XXXXXXXXXX@XXX.com';
let payload = {
    user: {
        type: 'user',
        name: 'test',
        email: 'test@gmail.com',
        role: 'Manager'
  }
};
var res = pd.users.createUser(from,  payload);
console.log(res);

1 Answers1

0

I believe the node-pagerduty client is promise based. Therefore the API functions return a Promise that you have to call .then() on or use await.

If you try:

pd.users.createUser(from,  payload).then((result) => {
    console.log('Result: ', result);
}).catch((error) => {
    console.log("Error: ", error);
});

You should get a result!

If you wish to use the async / await pattern you can try:

async function testCreateUser() {

    const pdClient = require('node-pagerduty');
    const pdApiKey = 'XXXXXXXXXXXXXX';
    const pd = new pdClient(pdApiKey);
    let from = 'XXXXXXXXXX@XXX.com';
    let payload = {
        user: {
            type: 'user',
            name: 'test',
            email: 'test@gmail.com',
            role: 'Manager'
      }
    };
    var res = await pd.users.createUser(from,  payload);
    console.log(res);

}

testCreateUser();
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40