Cannot get serverless iparams on localhost

Hello,

I’d like to save my mongodb connection password to iparams. I am receiving the error TypeError: cannot read properties of undefined (iparams)

This is my iparams.json

{
  "mongodb_password": {
    "display_name": "Mongdb password",
    "description": "Please enter the contact details",
    "type": "text",
    "required": true
  }
}

I am entering the password here http://localhost:10001/custom_configs

this is my server.js

var client;

const mongoose = require('mongoose')
mongoose.connect(`mongodb+srv://<username>:${client.iparams.get("mongodb_password")}@cluster0.320uhjy.mongodb.net/?retryWrites=true&w=majority`)

i have also tried the end-to-end testing

Are you using a custom installation page (config/iparams.html) to set your iparams? If so, make sure that you get access to the client object from the return value of app.initialized() call.

Once the MongoDB password is successfully stored in iparams, you must change the code to mongoose.connect(). client.iparams.get() returns a Promise. So, you must resolve it before calling mongoose.connect().

Note that you cannot require a node module from a frontend app and must use serverless functions to load mongoose and call the remote MongoDB server. Plus, client.* methods are only accessible from frontends. Serverless functions will give you access to iparam values as part of the event payloads.

1 Like

Thank you for the response Kaustav,

I don’t believe I am using a custom installation page. I am running a serverless application and using the url provided in the documentation: http://localhost:10001/custom_configs. I have also tried the end-to-end testing method, by going into Manage Apps → In development → Install app. This then prompts the iparams screen and I enter the password there.

This is my understanding as well. This is a serverless app, and the mongodb connection is run there. However, due to it being serverless I am unsure how to get the appropriate iparam values since the client object appears to only be on the front-end. You mentioned this is part of the event payloads object, but I do not see it in the provided test data. Do you have documentation on how I can access this?

Here is some example data I am working with

  "data": {
    "agent_activity": {
      "status": "AVAILABLE",
      "emoji": "joy",
      "availability_event_type": "Intelliassign",
      "status_change_timestamp": "2022-07-29T06:23:03.081Z",
      "custom_status_id": "75c23bdd-0e88-f234-417a-d3e2eb14c7bf",
      "org_agent_id": "374207180722084617",
      "email": "abc@freshchat.com",
      "prev_status": "UNAVAILABLE",
      "prev_status_change_timestamp": "2022-07-29T06:18:21.068Z"
    },
    "actor": {
      "type": "agent",
      "org_actor_id": "374207180722084617",
      "sub_entity": "hallway_bot",
      "id": "98c76ba0-6b38-499b-9c37-e1047742778c",
      "first_name": "Janr",
      "last_name": "Doe",
      "email": "john.doe@gmail.com",
      "phone": "9876543212",
      "avatar": {
        "url": "https://s3.amazonaws.com/fresh-chat-names/img/john_doe.png"
      },
      "social_profiles": [
        {
          "type": "facebook",
          "id": "john.doe"
        }
      ],
      "org_contact_id": "1556375110506151936",
      "login_status": false
    }
  },
  "region": "US",
  "account_id": "227655214724132",
  "domain": "web.freshchat.com",
  "event": "onAgentActivityCreate",
  "timestamp": 1564392329214,
  "version": "2.0.0"
}```

Hello @Luke_Padawer ,
I think you are accessing just the data part from the event payload. Can you share the callback function with in server.js, the default structure of event payload should be something like this.

{
  payload: {
    data: { agent_activity: [Object], actor: [Object] },
    region: 'US',
    account_id: '232,
    domain: 'domain.freshchat.com',
    event: 'onAgentActivityCreate',
    timestamp: 1564392319214,
    version: '2.0.0',
    iparams: {
      api_key:"test"
   }
  }
}

I would recommend you to create a sample serverless-app using fdk create > freshsales > your_first_serverless_app. There are many examples which can help.

2 Likes

Thank you,

I understand now. I was thinking that the iparams prop was in the test-data.json files, but it is not. I had to console.log(payload) inside my cb function in order to see it.

1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.