Search code examples
node.jsibm-cloudpersonality-insights

Personality insight input var nodejs


var PersonalityInsightsV2 = require('watson-developer-cloud/personality-insights/v2');

var personality_insights = new PersonalityInsightsV2({
  username: '<username>',
  password: '<password>'
});

personality_insights.profile({
  text: '<?php echo $_Session['description'];?>',
  language: 'en' },
  function (err, response) {
    if (err)
      console.log('error:', err);
    else
      console.log(JSON.stringify(response, null, 2));
});

It doesn't display anything. I have also done npm watson cloud and saved it, I have put my credentials and also forked it on git. What am I missing? I am a beginner but would love to use this on my page!


Solution

  • Here are the steps to run it locally, since you are a beginner I'll start from the beginning.

    Create a new folder and name it whatever you want. Put these files in there.

    Name the first file: index.js

    fill in <YOUR-USERNAME>, <YOUR-PASSWORD>, and <YOUR-100-UNIQUE-WORDS> variables.

    var express = require('express');
    var app = express();
    var http = require('http').Server(app);
    var cfenv = require("cfenv");
    
    var appEnv = cfenv.getAppEnv();
    
    http.listen(appEnv.port, appEnv.bind);
    
    var PersonalityInsightsV2 = require('watson-developer-cloud/personality-insights/v2');
    
    var personality_insights = new PersonalityInsightsV2({
      username: '<YOUR-USERNAME>',
      password: '<YOUR-PASSWORD>'
    });
    
    personality_insights.profile({
      text: "<YOUR-100-UNIQUE-WORDS>",
      language: 'en' },
      function (err, response) {
        if (err)
          console.log('error:', err);
        else
          console.log(JSON.stringify(response, null, 2));
    });
    

    Create another file and name it: package.json

    put these contents in there

    {
      "name": "myWatsonApp",
      "version": "1.0.0",
      "description": "A Watson Personality Insights application",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "dependencies": {
        "cfenv": "^1.0.3",
        "express": "^4.13.4",
        "watson-developer-cloud": "^2.2.0"
      }
    }
    

    open your terminal and cd to the root of your folder you just created.

    Run the command: npm install

    Then run the command npm start

    Your application will then be running and you will see output from the personality insights call you made in index.js