Object #<Object> has no method 'type' - node.js

My test code is
var strategy = require('../lib');
var should = require("should");
describe('passport-twitter', function() {
it('should export Strategy constructor directly from package', function() {
console.log('strategy is',strategy);
strategy.should.have.type('function');
});
it('should export Strategy constructor', function() {
strategy.Strategy.should.have.type('function');
});
});
and when i run this test.it is giving me error
1) passport-twitter should export Strategy constructor directly from package:
TypeError: Object #<Object> has no method 'type'
at Context.<anonymous> (/home/ritesh/projects/passport-topcoder/test/module.test.js:7:26)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:358:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:404:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:284:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:293:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:237:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
2) passport-twitter should export Strategy constructor:
TypeError: Object #<Object> has no method 'type'
at Context.<anonymous> (/home/ritesh/projects/passport-topcoder/test/module.test.js:11:35)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:358:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:404:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:284:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:293:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:237:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
but the type method is specified in documentation.how to get rid of this error.please help a bit.
instead of this when i try to run
aa
var strategy = require('..');
var should = require("should");
describe('passport-twitter', function() {
it('should export', function() {
console.log('strategy is',strategy);
'xxxx'.should.have.type('function');
});
});
still i am getting the error
1) passport-twitter should export:
TypeError: Object #<Object> has no method 'type'
at Context.<anonymous> (/home/ritesh/projects/passport-topcoder/test/module.test.js:7:23)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:358:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:404:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:284:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:293:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:237:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)

should.js has the following syntax:
'xxxx'.should.be.type('string');
Should.js documentation # type

Related

Querying firestore database returns "TypeError: Cannot read property '_internalPath' of undefined" as an error in console

I'm trying to get firestore to work with my node.js server, however it keeps returning this error :
TypeError: Cannot read property '_internalPath' of undefined
at fieldPathFromArgument (.\CS_Project\Source\node_modules#firebase\firestore\dist\index.node.cjs.js:21877:30)
at where (.\CS_Project\Source\node_modules#firebase\firestore\dist\index.node.cjs.js:22246:19)
at searchUsername (.\CS_Project\Source\server.js:54:33)
at .\CS_Project\Source\server.js:78:19
at Layer.handle [as handle_request] (.\CS_Project\Source\node_modules\express\lib\router\layer.js:95:5)
at next (.\CS_Project\Source\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (.\CS_Project\Source\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (.\CS_Project\Source\node_modules\express\lib\router\layer.js:95:5)
at .\CS_Project\Source\node_modules\express\lib\router\index.js:281:22
at Function.process_params (.\CS_Project\Source\node_modules\express\lib\router\index.js:335:12)
I Believe that the issue is with the below function however I am still relatively new to using Node.js so it may be an issue elsewhere in my code.
async function searchUsername(username){
try{
const q = query(userDb, where("username" == username));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}
catch(err){
console.log(err);
}
};
The closest question I could find was this, however when I tried their solution it didn't work.
The below error and code is from implementing the commented solution by #Dharmaraj.
const q = query(userDb, where("username", ==, username));
SyntaxError: Unexpected token '==' at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47
async function searchUsername(username){
try{
const q = query(userDb, where("username", ==, username));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}
catch(err){
console.log(err);
}
};
Thanks in advance for any help.

NodeJS with express, can't load model

I'm using NodeJS with express, consign, body-parser, etc.
I'm trying to load a module in a var, so i can use their functions, but i'm getting a:
TypeError: Cannot read property 'listar' of undefined
at app.get (/home/sartori/Documentos/Projetos/react-system/api/controllers/autores.js:6:11)
at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5)
at next (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5)
at /home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:335:12)
at next (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/index.js:275:10)
at /home/sartori/Documentos/Projetos/react-system/api/node_modules/express-validator/lib/express_validator.js:250:5
at Layer.handle [as handle_request] (/home/sartori/Documentos/Projetos/react-system/api/node_modules/express/lib/router/layer.js:95:5)
Here's my controller:
module.exports = app => {
app.get('/api/autores', (req,res) => {
let connection = app.connection.connectionFactory();
let autores = app.models.AutoresDao(connection);
autores.listar((error,result) => {
if(error) {
console.log('Erro ao listar os autores: ' + error);
res.status(500).send(error);
} else {
res.json(result);
}
});
});
}
Here's the AutoresDao:
function AutoresDao(connection) {
this._connection = connection;
}
AutoresDao.prototype.listar = callback => this._connection.query('SELECT * FROM autores', callback);
module.exports = function() {
return AutoresDao;
}
The problem is: Cannot read property 'listar' of undefined.
Apparently the problem is loading the AutoresDao file.
I'm 100% sure of files path, AutoresDao is inside models folder, already loaded on consign. The same for the controller, connection, express config etc.
Any idea of what i'm doing wrong?
You need to be using the 'new' keyword if you are going to format your object constructor function in that manner. You will have to reformat your AutoresDao function to explicitly return an object, or use the 'new' keyword when you assign it to autores in the controller.
Please checkout this article for an explanation of the different instantiation patterns.
https://medium.com/dailyjs/instantiation-patterns-in-javascript-8fdcf69e8f9b
Hope that helps! Cheers.

GET response data from request promise giving error

Using request promise module to do a post API call. The code is to get 'city' detail from the google maps api JSON output as below:
var rp = require('request-promise');
............
app.get('/check_promise', function(req,res) {
var option1 = {
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=Florence',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: true
}
rp(option1)
.then(function(response) {
console.log(response.data.results[0].formatted_address);
})
.catch(function(error){
console.log(error);
});
});
But the
console.log(response.data.results[0].formatted_address);
gives the below error:
TypeError: Cannot read property 'results' of undefined
at C:\Users\Laxmi\devapp\server.js:208:33
at tryCatcher (C:\Users\Laxmi\devapp\node_modules\bluebird\js\release\util.j
s:16:23)
at Promise._settlePromiseFromHandler (C:\Users\Laxmi\devapp\node_modules\blu
ebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\Users\Laxmi\devapp\node_modules\bluebird\js\re
lease\promise.js:569:18)
at Promise._settlePromise0 (C:\Users\Laxmi\devapp\node_modules\bluebird\js\r
elease\promise.js:614:10)
at Promise._settlePromises (C:\Users\Laxmi\devapp\node_modules\bluebird\js\r
elease\promise.js:693:18)
at Async._drainQueue (C:\Users\Laxmi\devapp\node_modules\bluebird\js\release
\async.js:133:16)
at Async._drainQueues (C:\Users\Laxmi\devapp\node_modules\bluebird\js\releas
e\async.js:143:10)
at Immediate.Async.drainQueues (C:\Users\Laxmi\devapp\node_modules\bluebird\
js\release\async.js:17:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
How to resolve the error to console log the city data?
console.log(response.results[0].formatted_address)

Nodejs middleware .pre shows not a function

var categoryList = new Referral({categoryList : category});
categoryList.pre('save', function (next) {
Referral.find({categoryList : category}, function (err, docs) {
if (!docs.length){
next();
}else{
console.log('Data exists: ', category);
next(new Error("Data exists!"));
}
})
})
Referral is my variable assigned to my schema.
categoryList is the object
This gives an error
TypeError: categoryList.pre is not a function at
D:\Aventyn\ClipCare_v2\app\api.js:112:18 at Layer.handle [as
handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:277:22
at Function.process_params
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:330:12)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:271:10)
at Function.handle
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:176:3)
at router
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:46:12)
at Layer.handle [as handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:312:13)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:280:7
at Function.process_params
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:330:12)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:271:10)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:618:15
Try changing:
categoryList.pre('save', function (next) {
// ...
})
to:
categoryList.schema.pre('save', function (next) {
// ...
})
The .pre() is a method of the Mongoose schema, not a model.
Middleware (like pre hooks) are part of a schema. It looks like you're trying to use it on a single document, which is not how it works.
Instead, use it on the schema that you used to create the Referral model:
ReferralSchema.pre('save', ...);
This does mean that the pre hook will be applied to all documents of that schema.

Cannot read property 'prototype' of undefined

I'm starting with Parse and Node.js. I'm trying to retrieve data from Parse db and I don't manage to.
var Parse = require('parse').Parse;
Parse.initialize("*************", "*****************");
module.exports = {
index: function(req, res, next){
var query = new Parse.Query(Parse.Event);
query.find({
success: function(results) {
console.log(results);
},
error: function(error) {
console.log(error.message);
}
});
}
}
And I got this error :
TypeError: Cannot read property 'prototype' of undefined
at Object.Parse.Query (/Users/tchiss_a/test/webapp/node_modules/parse/build/parse-latest.js:7771:33)
at module.exports.index (/Users/tchiss_a/test/webapp/api/Events.js:7:20)
at Layer.handle [as handle_request] (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/layer.js:82:5)
at /Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:235:24
at Function.proto.process_params (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:313:12)
at /Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:229:12
at Function.match_layer (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:296:3)
Can someone help me please ?
There is a fix listed in the comments, but the actual problem here is that you're treating a user-defined class "Event" like a Parse class.
change
var query = new Parse.Query(Parse.Event);
to
var query = new Parse.Query("Event");

Resources