From a college suggestion and following this answer
I'm trying to monkey patch res.send but I get the following error:
TypeError: Cannot read property 'req' of undefined
This is my code
const express = require('express')
const app = express()
app.use((req, res, next ) => {
const oldSend = res.send;
res.send = (data) => {
console.log(data.length);
oldSend(data);
}
next();
})
app.get('/', (req, res) => res.send('Hello World!'))
Full stacktrace:
Example app listening on port 3000!
undefined
TypeError: Cannot read property 'req' of undefined
at send (/Users/code/js/hello/node_modules/express/lib/response.js:110:17)
at ServerResponse.res.send (/Users/code/js/hello/index.js:8:9)
at app.get (/Users/code/js/hello/index.js:12:32)
at Layer.handle [as handle_request] (/Users/code/js/hello/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/code/js/hello/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/code/js/hello/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/code/js/hello/node_modules/express/lib/router/layer.js:95:5)
at /Users/code/js/hello/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/code/js/hello/node_modules/express/lib/router/index.js:335:12)
at next (/Users/code/js/hello/node_modules/express/lib/router/index.js:275:10)
Where line 110 is:
res.send = function send(body) {
var chunk = body;
var encoding;
var req = this.req; //<-- THIS
var type;
// settings
var app
....
It's because methods are not tied to their instances in JavaScript.
If you call a.fun(), inside the function code, this will initially be set to a. But this happens only because a appears in the method call: fun is otherwise an arbitrary function that has no relationship with a.
In your case, this should work:
oldSend.call(res, data);
The point of call() is to set this.
Found the solution, the oldSend loses it's context(?)
If I add bind(res):
const oldSend = res.send.bind(res);
The problem is gone.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I'm trying to build a torrent streaming api-thing, that streams you video files you download with torrents.
Here's my code:
import torrentStream from 'torrent-stream';
import rangeParser from 'range-parser';
import express from 'express';
const app = express();
const magnetURI = 'magnet:?xt=urn:btih:331B74F12A1CE8D00F0F8BE0844F5CC6471C925E&dn=The.Last.of.Us.S01E04.1080p.HMAX.WEBRip.DDP5.1.Atmos.x264-SMURF%5BTGx%5D&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=udp%3A%2F%2Fipv4.tracker.harry.lu%3A80%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.birkenwald.de%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fopentor.org%3A2710%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2970%2Fannounce&tr=https%3A%2F%2Ftracker.foreverpirates.co%3A443%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce';
const engine = torrentStream(magnetURI, {
path: './downloads', // location to save the downloaded file
});
engine.on('ready', () => {
const files = engine.files;
// Only select the first video file (you may need to modify this depending on your torrent)
const videoFile = files.find((file) => {
return file.name.endsWith('.mp4') || file.name.endsWith('.mkv') || file.name.endsWith('.avi');
});
if (!videoFile) {
console.log('No video files found in the torrent.');
return;
}
console.log(`Streaming ${videoFile.name} (${videoFile.length} bytes)`);
app.get('/', (req, res) => {
// Set the content type of the response to the video MIME type
res.contentType(videoFile.name.split('.').pop());
// Get the range of bytes to stream from the request headers
const range = req.headers.range;
const parts = rangeParser(videoFile.length, range);
// Set the content range of the response to the range being sent
res.set('Content-Range', `bytes ${parts[0].start}-${parts[0].end}/${videoFile.length}`);
// Stream the file to the response
const stream = videoFile.createReadStream(parts[0]);
stream.pipe(res);
});
app.listen(3000, () => {
console.log('Server listening on port 3000.');
});
});
engine.on('download', (pieceIndex) => {
console.log(`Downloaded piece ${pieceIndex}.`);
});
engine.on('idle', () => {
console.log('Download complete.');
});
but it gave me this error:
TypeError: argument str must be a string
at rangeParser (G:\STREAMING-THING\node_modules\range-parser\index.js:29:11)
at file:///G:/STREAMING-THING/what.js:33:19
at Layer.handle [as handle_request] (G:\STREAMING-THING\node_modules\express\lib\router\layer.js:95:5)
at next (G:\STREAMING-THING\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (G:\STREAMING-THING\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (G:\STREAMING-THING\node_modules\express\lib\router\layer.js:95:5)
at G:\STREAMING-THING\node_modules\express\lib\router\index.js:284:15
at Function.process_params (G:\STREAMING-THING\node_modules\express\lib\router\index.js:346:12)
at next (G:\STREAMING-THING\node_modules\express\lib\router\index.js:280:10)
at expressInit (G:\STREAMING-THING\node_modules\express\lib\middleware\init.js:40:5)
I am trying to make a simple website with Firebase Functions.
I have to display a prompt to ask their import id.
I will then set the answer as a variable.
Then import their data with the import code
Following is that code in index.js
async function getFirestore() {
var importcode = prompt("What is the Import Code?");
const firestore_con = await admin.firestore();
const writeResult = firestore_con
.collection("Exports")
.doc(importcode)
.get()
.then((doc) => {
if (!doc.exists) {
console.log("No such document!");
} else {
return doc.data();
}
})
.catch((err) => {
console.log("Error getting document", err);
});
return writeResult;
}
and I get this in the Debug Log.
i functions: Beginning execution of "us-central1-app"
> /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19
> var importcode = prompt("What is the Import Code?");
> ^
>
> ReferenceError: prompt is not defined
> at getFirestore (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19:22)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:39:27
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:137:13)
> at Route.dispatch (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:112:3)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:281:22
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at expressInit (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/init.js:40:5)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at trim_prefix (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:317:13)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:284:7
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at query (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/query.js:45:5)
I think this happens because index.js runs on the server-side, but are there any alternatives to achieve this?
PS: I only have very basic knowledge in firebase, so this may be a foolish question but still please help :)
The prompt() method can be used in a browser and not on server side i.e. in the Cloud function. I am not sure what your frontend looks like, but you should prompt the user to enter ID in your web app and then pass it in the Cloud function. A simple example using callable functions:
var importcode = prompt("What is the Import Code?");
var getData = firebase.functions().httpsCallable('getData');
getData({ code: importCode })
.then((result) => {
// Read result of the Cloud Function.
var result = result.data;
console.log(result)
});
Then you can read this code in your Cloud function like this:
exports.getData = functions.https.onCall((data, context) => {
console.log(data)
// data contains the import code
// run your Firestore query here
});
I've recently come across a problem when working with the built-in req.accepts, req.acceptsLanguages, req.acceptsCharsets, and req.acceptsEncodings functions in express.
I have an express middleware function like this:
function acceptCheckpoint(acceptOpts) {
// Calling the following function results in a TypeError.
function checkAccept(req, res, opts) {
let acceptFunction = null;
switch (opts.whichAccept) {
case "type":
acceptFunction = req.accepts;
break;
case "lang":
acceptFunction = req.acceptsLanguages;
break;
case "charset":
acceptFunction = req.acceptsCharsets;
break;
case "encoding":
acceptFunction = req.acceptsEncodings;
break;
default:
acceptFunction = req.accepts;
break;
}
return acceptFunction(opts.acceptedTypes);
}
return (req, res, next) => {
const accepted = [];
Object.getOwnPropertyNames(acceptOpts).forEach(key => {
if (key === "ignoreAcceptMismatch") { return; }
const acceptsType = checkAccept(req, res, {
whichAccept: key,
acceptedTypes: acceptOpts[key]
});
accepted.push(acceptsType);
});
if (accepted.some(type => !type) && !acceptOpts.ignoreAcceptMismatch) {
res.type("html");
res.status(406);
res.send("<h1>406 Not Acceptable.</h1>");
return;
}
next();
};
}
Which, in theory, should work. But the program keeps complaining and logs this error:
TypeError: Cannot read property 'headers' of undefined
at new Accepts (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/accepts/index.js:37:22)
at Accepts (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/accepts/index.js:34:12)
at req.accepts (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/express/lib/request.js:133:16)
at checkAccept (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/Lib/middleware.js:208:12)
at /Users/hortoncheng/Desktop/Programs/colonialwars/dev/Lib/middleware.js:216:27
at Array.forEach (<anonymous>)
at /Users/hortoncheng/Desktop/Programs/colonialwars/dev/Lib/middleware.js:214:44
at Layer.handle [as handle_request] (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/express/lib/router/index.js:317:13)
at /Users/hortoncheng/Desktop/Programs/colonialwars/dev/node_modules/express/lib/router/index.js:284:7
The thing is, when I use req.accepts or one of those .accepts functions in the main function (acceptCheckpoint), like this:
// Pretend we're in acceptCheckpoint...
// This works.
accepted.push(req.accepts("html"));
It works. And, when I log the req object in either of those functions, it returns the expected value. I've also tried logging the req object in the request.js file of the express module, and there, it returned undefined. So that led me to believe that it was a problem with express itself. I tried deleting package-lock.json and node_modules, and then running npm install. Didn't fix it. And yes, I'm calling the express middleware function correctly. Any idea why this is happening?
I'm using express v4.17.1, Node.JS v12.18.1, and NPM v6.14.5.
The function is presumably trying to get req from its this context. But you're not passing functions with context.
Change this line:
return acceptFunction(opts.acceptedTypes);
to:
return acceptFunction.call(req, opts.acceptedTypes);
The first argument to the call() method is the object that should be used as this in the called function.
I am trying to implement a transaction processor in javascript SDK based on the following example
https://github.com/hyperledger/sawtooth-core/blob/master/sdk/examples/intkey_javascript/index.js
Here is my code to run a transaction processor in javascript SDK
//validator public key
const validatorAddress = '024c512a6d66917d7d00f52fa299a88594915dab27bddbcd2a80154984d7948c3c';
const IntegerKeyHandler = require('./handler');
const startProcessor = function startProcessor(){
const transactionProcessor = new TransactionProcessor(validatorAddress);
transactionProcessor.addHandler(new IntegerKeyHandler())
transactionProcessor.start()
}
But i am getting invalid argument error
Error: Invalid argument
at exports.Socket.Socket.connect (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/zeromq/lib/index.js:510:13)
at Stream.connect (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/sawtooth-sdk/messaging/stream.js:85:18)
at TransactionProcessor.start (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/sawtooth-sdk/processor/index.js:72:18)
at Object.startProcessor (/var/accubits-workspace/hypeerledger-sawtooth/tuts/helpers/transaction-processor.js:15:26)
at app.get (/var/accubits-workspace/hypeerledger-sawtooth/tuts/index.js:62:26)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at next (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at /var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:281:22
Change the validator address to the url of the validation which can be either tcp://validator:4004 or tcp://localhost:4004
Here's the full code:
'use strict'
const { TransactionProcessor } = require('sawtooth-sdk/processor')
const IntegerKeyHandler = require('./integer_key_handler')
const address = 'tcp://validator:4004' // If you are not running it inside docker container then change the address to this tcp://localhost:4004
const transactionProcessor = new TransactionProcessor(address);
transactionProcessor.addHandler(new IntegerKeyHandler());
transactionProcessor.start();
I've come across a very peculiar bug that I can't seem to solve. In Node.js, I have an API wrapper library called Markitable:
var request = require('request');
var Q = require('q');
/**
* Markit on Demand API Client
* #constructor
* #author Chandler Freeman <chandler.freeman#gmail.com>
*/
var Markitable = function() {
};
module.exports = Markitable;
endpoints = {
lookup: 'http://dev.markitondemand.com/Api/v2/Lookup',
quote: 'http://dev.markitondemand.com/Api/v2/Quote',
interactivechart: 'http://dev.markitondemand.com/Api/v2/InteractiveChart'
};
/**
* Company lookup
* #httpMethod GET
* #param {ticker} String A string containing the ticker for the stock
* #param {callback} callback (Optional) If callback is passed, than the function will use callbacks
* #promises yes By default, this function uses promises
*/
Markitable.prototype.lookup = function(ticker, callback) {
var options = {
url: endpoints.lookup,
headers: this.headers,
qs: { 'input': ticker.toUpperCase() },
method: 'GET',
json: true
};
if (!callback) {
var d = Q.defer();
request(options, function(err, httpResponse, body) {
if (err) return d.reject(err);
d.resolve(body);
});
return d.promise;
}
else
request(options, callback);
};
As you can see, all it does is fetch data from the Markit on Demand API and return a Q promise. However, every time I include the
var Markitable = require('markitable');
statement anywhere in my code, when I navigate to the / route of my application I receive
Failure on / route: TypeError: Cannot read property 'user' of undefined
I am using Express I have absolutely no idea at all why this is happening; I've reread the source several times, checked source control changes, everything, yet I can't find the root of the issue. This behavior only persists when the library is included; as soon as I remove that statement, everything works perfectly. I don't understand because the code for this library was the exact same code I used from another library I wrote, and the first one looks great. Here is the code for my routes file:
var User = require('../app/models/user');
var Robinhood = require('marian');
var util = require('util');
var Q = require('q');
module.exports = function(app, passport) {
// =========================================
// Table of Contents(Search by name)
//
// 1. Root
//
// =========================================
// ********************* 1. Root *********************
app.get('/', isLoggedIn, asyncCallback(function*(req, res) {
rh = new Robinhood(req.user.local.rhtoken);
var viewData = { user : req.user };
try {
// e: <Cannot read property user of undefined>
viewData.rhUser = yield rh.getUser();
viewData.rhUser.basic_info = yield rh.getBasicInfo();
viewData.rhPortfolio = yield rh.getPortfolio();
viewData.getOrders = yield rh.getOrders();
viewData.rhAccount = yield rh.getAccount();
viewData.active = { page : 'dashboard' };
res.render('main_pages/maindashboard.ejs', viewData);
}
catch(e) {
console.log("Failure on / route: " + e);
res.render('meta/500.ejs');
}
}));
};
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/login');
}
// Code originally from James Long
// http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators
function asyncCallback(gen) {
return function() {
return Q.async(gen).apply(null, arguments).done();
};
}
Any ideas about why this strange behavior may be occurring? I can't imagine why importing a library would affect the 'req' object, but somehow it does. Could this be a bug in Express?
EDIT:
Forgot the stack trace:
TypeError: Cannot read property 'user' of undefined
at Robinhood.getUser (/vagrant/StockFire/node_modules/marian/index.js:110:26)
at /vagrant/StockFire/app/routes.js:28:40
at next (native)
at Function.continuer (/vagrant/StockFire/node_modules/q/q.js:1278:45)
at /vagrant/StockFire/node_modules/q/q.js:1305:16
at /vagrant/StockFire/app/routes.js:226:29
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at isLoggedIn (/vagrant/StockFire/app/routes.js:217:16)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/vagrant/StockFire/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at /vagrant/StockFire/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/vagrant/StockFire/node_modules/express/lib/router/index.js:330:12)
at next (/vagrant/StockFire/node_modules/express/lib/router/index.js:271:10)
Solved the issue. Simple bug, but very hard to find. I discovered that a variable named endpoints existed in both libraries, but neither was declared with var, meaning that the variable was overwritten since they both existed in the javascript global scope. Lessons learned: Always check variable scope.