ReferenceError: request not defined

I am trying to replicate facebook messenger bot but keep getting request is not defined .

Same code as facebook:

 function callSendAPI(messageData) { request({ uri: 'https://graph.facebook.com/v2.6/me/messages', qs: { access_token: PAGE_ACCESS_TOKEN }, method: 'POST', json: messageData }, function (error, response, body) { if (!error && response.statusCode == 200) { var recipientId = body.recipient_id; var messageId = body.message_id; console.log("Successfully sent generic message with id %s to recipient %s", messageId, recipientId); } else { console.error("Unable to send message."); console.error(response); console.error(error); } }); } 

My node server.js looks like this:

 const express = require('express'); const bodyParser = require('body-parser'); //const request = express.request; const PAGE_ACCESS_TOKEN = 'abc'; let app = express(); app.use(bodyParser.urlencoded({ extended: false })); // parse application/json app.use(bodyParser.json()); [...] function sendTextMessage(recipientId, messageText) { var messageData = { recipient: { id: recipientId }, message: { text: messageText } }; callSendAPI(messageData); } function callSendAPI(messageData) {..} [...] 

Am I missing something with an expression? Thanks

+8
source share
2 answers

This example uses a third-party query module .

You can also use your own request like this: require('http').request() if you want, but I would say that the request module is very common, and a good tool to use.

EDIT Actually, I just noticed that you have this in the code, but he commented! After a longer look, your comment request points to express.request , which, if used as request() , throws an error, since this is not a function. So you really have to use the Request module or configure the code to use native http.request .

+3
source

You do not have the request module installed.

First install it npm install --save request , and then enable it var request = require('request');

0
source

Source: https://habr.com/ru/post/1264467/


All Articles