Saturday, 1 April 2017

node.js Facebook SDK OAuthException

I want to create a server with node.js to cache data like public page feed from Facebook graph API. I need this, because I wanted to make an Android app which pulls a page feed with app access code, but because the app secret should not be used in such app which can be decompiled, I must have an dedicated application server

I'm trying to use tenorviol's node.js SDK but I also have tried all other SDK's for node.js listed on the website of facebook. But I couldn't run any code without getting request or oAuth errors.

I took the app id and secret (MYAPPID and MYAPPSECRET respectively) from my profile page at developers.facebook.com

Also, is there a better way to do this? I'm thinking is "node.js + facebook Node.js SDK + NoSQL" faster than "Apache + NoSQL + PHP facebook SDK" for caching results?

Example:

var fbsdk = require('facebook-sdk');

var facebook = new fbsdk.Facebook({
  appId  : ''MYAPPID',
  secret : 'MYAPPSECRET'
});

facebook.api('/me', function(data) {
  console.log(data);
});

Then I run with node index.js:

{ result:
   { error:
      { message: 'An active access token must be used to query information about the current user.',
        type: 'OAuthException',
        code: 2500,
        fbtrace_id: 'DPWsuCVegfz' } },
  error: true,
  code: 0,
  message: 'An active access token must be used to query information about the current user.' }



via Classicpal

Natively access JSON POST payload from Node.js server

Consider the following HTTP POST call to a Node.js server:

curl -H "Content-Type: application/json" \
     -X POST \
     -d '{"jsonKey":"jsonValue"}' \
     'http://localhost:8080?abcd=efgh'

I would like to access both the URL parameters and the JSON payload of the POST request.

Accessing the URL params is pretty straightforward by importing url.parse:

var server = http.createServer(function(req, res) {
        // Parse the params - prints "{ abcd: 'efgh' }"
        var URLParams = url.parse(req.url, true).query;
        console.log(URLParams);

        // How do I access the JSON payload as an object?
}

But how do I access the JSON payload, using native Node.js library (without any npm import)?

What have I tried

  • Printed req to console.log, but did not find the POST object
  • Read the documentation of req, which is of type http.IncomingMessage


via Adam Matan

Pool error whenever I try and launch my js file

I have this error whenever I launch my js file with node, first I thought it was maybe my database connection but that seems to be right..

Here is the error,

enter code here
at Parser.write (/home/bot/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/bot/node_modules/mysql/lib/Connection.js:103:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at PoolConnection.connect (/home/bot/node_modules/mysql/lib/Connection.js:130:18)
at Pool.getConnection (/home/bot/node_modules/mysql/lib/Pool.js:48:16)
at query (/home/bot/site.js:566:7)
at load (/home/bot/site.js:578:2)
at Object.<anonymous> (/home/bot/site.js:58:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)



via Martin Dev

Determine what shell (cmd, bash, or etc) was used to run node.js script

As the title states, I'd like determine what shell (cmd, bash, or etc) was used to run the node.js script (from within the script).

While detecting the OS with the os module can give some clues, it is not enough is some instances, such as as if I were executing from bash on win32 through a terminal like GitBash or Cygwin.

Is there a property somewhere on the process object that has this information?



via bitstrider

mongoose Getting a nested field value from a queried document

I am new to Node js and Mongoose and I am stuck with this one query.

exports.getIndividual=(matchId,phone)=>
 new Promise((resolve,reject)=>{
 var e="matchPersonal.m"+matchId+".bo1";
 query={};
 query[e]=1;

 var quer=usersc.findOne({phone:"8989898980"}).select(query);
 quer.exec(function (err, matc) { 

 var m=matc.toObject();

 console.log('',m);

When I Run this query I get on my console this thing

        {matchPersonal:{m1:{bo1:3}}}

But I just need 3 as my output and I cant use dot operator to read the object properties directly because

   var b=matchPersonal.m+matchId.bo1

doesnt work as '+' cant be used here so please help



via learn_ing

Node Express Can't set headers after they are sent

I know this has been asked in multiple ways already, but my issue seems to be different from those already posed.

I'm trying to send data from a form into google firebase. I have a node app using express.

Here's my function that is sending the data to firebase:

function validateForm() {

async.series([
    function (callback) {
        var errors = "<strong>The following errors were entered:\n";
        var name = $('#name').val();
        var story = $('#story').val();

        if (name.length < 1) {
            errors += "\n-Please enter a valid name";
            callback("Please enter a valid name", null);
        }

        if (story.length < 1) {
            errors += "\n-Please enter a valid story";
            callback("Please enter a valid story", null);
        }
        console.log("FInished validating");
        callback(null, "finished validating");
    }, function (callback) {
        firebase.database().ref('stories/' + Date.now()).set({
            name: name,
            story: story
        }, function() {
            console.log("Firebase callback");
            callback(null, "sent data!");
        });
    }, function (callback) {
        console.log("Finished!");
        callback(null, "done")
    }
])

}

(added come console.logging for clarity to ensure I had the callbacks right)

The submission is trigger by clicking on a div that's styles to look like a button, so I know there's no default behavior of forms issues that are causing the problem. Below is the error I'm getting in Node.

Error: /Users/keegan/WebstormProjects/hack4health/views/error.hbs: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) at ServerResponse.header (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:719:10) at ServerResponse.send (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:164:12) at res.render.done (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:956:10) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:93:9 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:88:18 at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:69:11 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:64:20



via thekeegs

protractor without selenium server

When I issue the protractor command at the command line, with the following configuration:

I get this:

$ protractor
[12:22:23] I/launcher - Running 1 instances of WebDriver
[12:22:23] I/local - Starting selenium standalone server...
[12:22:24] I/local - Selenium standalone server started at http://10.0.0.242:55414/wd/hub
Started
.


1 spec, 0 failures
Finished in 8.223 seconds

[12:22:33] I/local - Shutting down selenium standalone server.
[12:22:33] I/launcher - 0 instance(s) of WebDriver still running
[12:22:33] I/launcher - chrome #01 passed

the problem is that it takes 5+ seconds to start up the "selenium standalone server".

Two questions - (1) do I need this server to run the tests? And (2), is there a way to run the server in the background without having to restart the server everytime?



via Alexander Mills