I have a variable var sessions={} in file called 'UserSessions.js'. I am maintaining some information of each user in sessions={} with a unique timestamp. I have exported this variable to make it available to other files.
When I access the sessions={} in main file 'app.js' everything works fine.
But when I try to access same variable from another file 'Sessiongreet.js', it gives error.
Here is how I access the data :
Suppose '2017-04-07T11:55:40.162Z' is the unique timestamp assigned only once.
In app.js:
This Works fine:
const UserSessions=require('./UserSessions.js');
sessionId='2017-04-07T11:55:40.162Z';
var data=UserSessions.sessions[sessionId].context;
In sessionGreet.js:
This gives error:
const UserSessions=require('./UserSessions.js');
sessionId='2017-04-07T11:55:40.162Z';
var data=UserSessions.sessions[sessionId].context;
I know that UserSessions.sessions[sessionId].context exists as it is accessible in app.js file before accessing it in another file.
Here is the exact error what I get :
TypeError: Cannot read property '2017-04-07T11:55:40.162Z' of undefined
at initSession (/media/row_hammer/sessionGreet.js:24:33)
at Object.run (/media/row_hammer/sessionGreet.js:67:2)
at Object.handlePostback (/media/row_hammer/sessionTemp.js:89:19)
at runPostback (/media/row_hammer/app.js:113:15)
at /media/row_hammer/app.js:161:3
at Object.findOrCreateSession (/media/row_hammer/UserSessions.js:83:4)
at Bot.bot.on (/media/row_hammer/app.js:159:15)
at emitThree (events.js:116:13)
at Bot.emit (events.js:194:7)
at Bot._handleEvent (/media/row_hammer/node_modules/messenger-bot/index.js:254:10)
Also, In sessionGreet.js:
//EVEN this line shows 'undefined'
console.log(UserSessions.sessions);
Why am I getting this error even though flow of program is correct?
It may be that app.js and Sessiongreet.js are in different folders.
'./UserSessions.js' means that UserSessions.js is in the same folder as the file issuing the require().
If app.js is in the same folder as UserSessions.js, but Sessiongreet.js is in different folder, this would explain your issue.
Related
Error screenshot:
While running react project by npm start, it shows error related to contentful.js. why does the package shows these types of error? I attached the screenshot with this post. How to solve this issue?
.env file
CONTENTFUL_ACCESS_TOKEN: process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN,
config file
import SETTINGS from "../env";
const contentfulClient = contentful.createClient({
space: SETTINGS.CONTENTFUL_SPACE,
accessToken: SETTINGS.CONTENTFUL_ACCESS_TOKEN
});
Do i need to enter api token anywhere?
.env
const SETTINGS = {
LANDING_BLOG_POSTS: "",
ICON_TYPE: "svg",
CONTENTFUL_SPACE: process.env.REACT_APP_CONTENTFUL_SPACE,
CONTENTFUL_ACCESS_TOKEN: process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN,
CLOUDINARY_UNSIGNED_PRESET: process.env.REACT_APP_CLOUDINARY_UNSIGNED_PRESET,
CLOUDINARY_CLOUD_NAME: process.env.REACT_APP_CLOUDINARY_CLOUD_NAME,
SERVER_7CHIP: process.env.REACT_APP_SERVER_7HIP || false,
HARD_CODED_USERNAME: process.env.REACT_APP_TOKEN_USERNAME || "**********",
HARD_CODED_PASSWORD: process.env.REACT_APP_TOKEN_PASSWORD || "************",
FACEBOOK_API_ID: process.env.REACT_APP_FACEBOOK_APP_ID,
FACEBOOK_PAGE_TOKEN: process.env.REACT_APP_FACEBOOK_PAGE_TOKEN,
FACEBOOK_PAGE_ID: process.env.REACT_APP_FACEBOOK_PAGE_ID || "**************",
FACEBOOK_SERVER_CALL: true
};
export default SETTINGS;
Since your code got as far as getting into the createClient function, that tells me your SETTINGS object is loading (otherwise you'd get a TypeError from accessToken: SETTINGS.CONTENTFUL_ACCESS_TOKEN). However the error message says that no accessToken was provided.
Therefore SETTINGS.CONTENTFUL_ACCESS_TOKEN is undefined, null or an empty string. Since that value is set to process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN this probably means that you have not set your REACT_APP_CONTENTFUL_ACCESS_TOKEN environment variable before running the program.
Try setting that environment variable to your access token value, then running the program.
If that works, you might want to reconsider the approach. You have an "env" file but is getting values from system environment variables. Usually you have an "env" file so that you can hardcode values into it, not rely on them being set externally. If they have to be set externally you might as well not use an "env" file and just use process.env.VARIABLE_NAME directly in your code.
Note that if you hardcode access tokens or other private information in an "env" file, you should add that file to .gitignore so that passwords/keys are not stored in git.
You created .env.development file same location as package.json file.
hope, this will help you.
For me, I had the variables in the .env file in the source directory and just had to source it. I ran . ./.env and that sorted out the error in my case.
In my case environment variables weren't accessible, because I created the .env file when my server was already running. I had to restart the server and everything worked like a charm.
I am requiring the JSON file to a variable using require(./).
Here I am testing a webpage where I am using wdio test runner.
The project directory structure is like this
practiseWebdriver.io/
/test --> contains test Scripts
/pageobjects --> contains json file to be read
When tried to use:
var pageObject = require('./pageobjects/abc.json');
I am facing error -> ERROR: Cannot find module './pageobjects/abc.json'
But the script is running fine when I try :
1. var pageObject = require('/home/{user}/Documents/VS_Workspace/practiseWebdriver.io/pageobjects/abc.json');
2. var pageObject = require(process.cwd()+'./pageobjects/abc.json');
Can you please help me to identify if there's anything I am missing?
use this : require('../pageobjects/abc.json');
I have trouble to build https://github.com/webrtc/samples in win 7.
I am following the steps of this Doc.
But there are some errors like as the following.
D:\demo\webrtcDemo\samples\test>node server.js
D:\demo\webrtcDemo\samples\test\server.js:20
key: keys.serviceKey,
^
TypeError: Cannot read property 'serviceKey' of undefined
at D:\demo\webrtcDemo\samples\test\server.js:20:14
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:278:24
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:168:24
at done (D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:78:24)
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:91:13
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1044:20
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1027:9
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:937:20
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1092:20
at F (D:\demo\webrtcDemo\samples\node_modules\pem\node_modules\which\which.j
s:68:16)
Thanks for any reply ^o^ !!!
I had the same problem and I solved that by installing OpenSSL in my machine.
I was using Windows 10 and this : https://www.youtube.com/watch?v=PYLTtABP1F4 worked for me.
It is important to setup Environment variables. One last step is to setup config.cnf file in Environment Variables. I had xampp so the path for that is "C:\xampp\apache\conf\openssl.cnf".
Set variable name : OPENSSL_CONF
and variable value : C:\xampp\apache\conf\openssl.cnf
In Linux(I used Ubuntu) it is easy, just follow these instructions : https://ubuntuforums.org/showthread.php?t=2110429
and set config.cnf file as per your server.
I'm new to Node.js, MongoDB and MongoJS.
I have app.js which is the server I run from my command line with node app.js. On a local machine with OS X 10.9.1 with Node.js v0.10.28.
In app.js I have (as snippets)
var db = require('./database');
var blog = require('./classes/blog');
and database.js has
var db = require("mongojs");
db.connect('mydb', ["Users", "Posts"]);
and /classes/blog.js has
function load(OnDone) {
if (db.connected) {
OnDone();
}
}
When I call blog.load() I get
ReferenceError: db is not defined
at Object.load (node/classes/blog.js:13:6)
at node/app.js:36:7
at Server.onRequest (node/server.js:35:12)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:527:27)
Everything in my app works as expected except when I try to call my load() function which uses the db var that I thought I declared in app.js.
Why would db be undefined? Am I using modules wrong?
Note: if you can't tell, I extracted these lines of codes from my scripts since my app so far is relatively long for a SO question and I don't want to show it all unless I need to.
I believe Node.js modules are contained so do not have access to variables defined in their parents (as I assumed).
I moved db to the global namespace (I removed var) and it's working now.
So I'm trying to teach myself Jasmine (for node) by working through a tutorial for a Mongoose project, TDD style, writing tests for what each step is supposed to accomplish, then following the actual tutorial, etc.
Of course, my first test is failing.
app.js at this point is two lines:
var mongoose = require('mongoose');
console.log(mongoose.version);
This runs fine. My test however, still fails:
var app = require('../src/app.js');
describe('App startup', function() {
it('loads mongoose', function() {
expect(app.mongoose.version).toBeDefined();
});
it('loads jasmine-jquery', function() {
expect($).toBeDefined();
});
});
Results in
Failures:
1) App startup loads mongoose
Message:
TypeError: Cannot read property 'version' of undefined
Stacktrace:
TypeError: Cannot read property 'version' of undefined
at null.<anonymous> (/home/jbhelfrich/mongooseBlog/spec/init.spec.js:5:36)
(The jquery test is, of course, expected to fail still at this point.) I've tried it with and without the 'app.' in the expect clause, but I get the same error--the test suite doesn't see the internals of app.js. But I know it's loading the app.js file correctly, because it's running it--the console.log output appears ahead of the test results.
So I suspect I've misunderstood something fundamental about scope, or some other rookie mistake, but I'm not sure what that is.
Node.js is structured into modules. If you want a a module's properties to be accessible, that module's properties must be defined in the module.exports variable. This is what an export might look like (note that exports references module.exports):
var mongoose = require('mongoose');
console.log(mongoose.version);
exports.mongoose = mongoose;
Then when you've used require() on a file with the code shown above, the variables will be set, where app is equivalent to module.exports in the module that is being loaded:
var app = require('../src/app.js');
console.log(app.mongoose.version);