Baucis - router documents method not found - node.js

I'm working on a new project and I'm new with baucis for node.js. This is the code:
var name = 'order'
, baucis = require('baucis')
, controller = baucis.rest(name)
, paths = config.paths;
controller.request(function (req, res, next) {
if (req.isAuthenticated())
return next();
return res.send(401);
});
controller.documents(function (req, res, next) {
if (typeof req.baucis.documents === 'number') return next();
[...]
next();
});
And this is the error:
/Users/fil/[...]/order.js:31
controller.documents(function (req, res, next) {
^
TypeError: Object function router(req, res, next) {
router.handle(req, res, next);
} has no method 'documents'
at Object.<anonymous> (/Users/fil/[...]/order.js:31:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/fil/[...]/app.js:29:5
at Array.forEach (native)
at Object.<anonymous> (/Users/fil/[...]/app.js:27:57)
Any idea of the reason why this happends?

Hard to say 100% for sure, but I am guessing you are trying to use the controller.documents method with a new version of baucis, one that does not support this middleware stage.
If this is so, read about using outgoing stream transforms in the README.md file.
HTH

Related

Cannot find module nodeJS

Hello I'm having a silly problem but I can't find the solution. When I try start my localhost I get an error saying this :
Error: Cannot find module 'passport'
Require stack:
- D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js
- D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js
- D:\Other\Projects\Code\Powershell\shopping-cart\app.js
- D:\Other\Projects\Code\Powershell\shopping-cart\bin\www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\config\\passport.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\routes\\index.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\app.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\bin\\www'
}
I understand that it is not finding the passport.js file, I have checked the path name more than 10 times and I saw another problem where the guy had not installed the passport module, so I did that as well but it is still throwing the same error. This is what my index.js looks like :
var express = require('express');
var router = express.Router();
var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');
var csrf = require('csurf');
var passport = require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');
var csrfProtection = csrf();
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
Product.find(function(err, docs) {
var productChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
});
});
router.get('/user/signup', function (req, res, next) {
res.render('user/signup', {csrfToken: req.csrfToken()});
});
router.post('/user/signup', passport.authenticate('local.signup', {
successRedirect: '/user/profile',
failureRedirect: '/user/signup',
failureFlash: true
}));
router.get('/user/profile', function(req, res, next) {
res.render('user/profile');
});
module.exports = router;
Any help would be appreciated, thanks! Also , I have another problem , can I post another question in like one hour? (is it allowed to have more than 1 questions posted?)

module.js:549 throw err;

> module.js:549
> throw err;
> ^
>
> Error: Cannot find module '..models/category'
> at Function.Module._resolveFilename (module.js:547:15)
> at Function.Module._load (module.js:474:25)
> at Module.require (module.js:596:17)
> at require (internal/module.js:11:18)
> at Object.<anonymous> (/home/mridul/shafee-it/routes/admin.js:2:18)
> at Module._compile (module.js:652:30)
> at Object.Module._extensions..js (module.js:663:10)
> at Module.load (module.js:565:32)
> at tryModuleLoad (module.js:505:12)
> at Function.Module._load (module.js:497:3)
> at Module.require (module.js:596:17)
> at require (internal/module.js:11:18)
> at Object.<anonymous> (/home/mridul/shafee-it/server.js:74:21)
> at Module._compile (module.js:652:30)
> at Object.Module._extensions..js (module.js:663:10)
> at Module.load (module.js:565:32) [nodemon] app crashed - waiting for file changes before starting...
Here is my ..models/category(category schema)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
name: { type: String, unique: true, uppercase: true}
});
module.exports = mongoose.model('Category', CategorySchema);
add-category Route in admin.js file
const router = require('express').Router();
const Category = require('..models/category');
// add-category route
router.get('/add-category', function(req, res, next) {
res.render('admin/add-category', { message: req.flash('success') });
});
router.post('/add-category', function(req, res, next) {
var category = new Category();
category.name = req.body.name;
category.save(function(err) {
if (err) return next(err);
req.flash('success', 'Successfully added a category');
return res.redirect('/add-category');
});
});
Here also server.js file to include the category
const Category = require('./models/category');
app.use(function(req, res, next) {
Category.find({}, function(err, categories) {
if (err) return next(err);
res.locals.categories = categories;
next();
});
});
const adminRoutes = require('./routes/admin');
app.use(adminRoutes);
when i run my server or route the add-category url then show module.js:549 throw err; this problem.
Here is my problem details and i check my code different times.
Then how can i solve this problem.
Seems to me that you forgot a /:
require('..models/category');
require('../models/category');
This is why ..models/category throws an error. From a general point of view, when require can't find a local module, don't "check the code", just check where the file is, and what path you wrote, it's always one or the other. ;)

Route.get() requires callback functions but got a [object Undefined] after using two get request

i am learning node and i have two get request in my index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.status(200)
.json({
status: 'success',
message: 'Welcome to EverestApi!'
});
});
var db = require('./queries');
router.get('/api/users', db.getAllUsers);
router.get('/api/pref', db.getAllPref);
module.exports = router;
and my query.js
var promise = require('bluebird');
var options ={
promiseLib : promise
};
var pgp = require('pg-promise')(options);
var db = pgp({
host: 'localhost',
port: 5432,
database: 'nari',
user: 'postgres',
password: 'pes'
});
function getAllUsers(req, res, next) {
db.any('SELECT * FROM office.users')
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved all users'
});
})
.catch(function (err) {
return next(err);
});
}
function getAllPref(req, res, next){
db.any('SELECT * FROM core.preferences')
.then(function(data){
res.status(200)
.json({
status: 'success',
data : data,
message : 'Retrieved all preferences'
});
})
.catch(function(err){
return next(err);
});
}
module.exports ={getAllUsers: getAllUsers};
module.exports={getAllPref : getAllPref};
I have two GET Action in here.One getAllUsers() and another getAllPref().Starting the server gives error .
D:\node-postgress-promises\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^
Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (D:\node-postgress-promises\node_modules\express\lib\router\route.js:202:15)
at Function.proto.(anonymous function) [as get] (D:\node-postgress-promises\node_modules\express\lib\router\index.js:510:19)
at Object. (D:\node-postgress-promises\api\index.js:17:8)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (D:\node-postgress-promises\server.js:6:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
If i comment one of the above routes for eg . router.get('/api/users', db.getAllUsers);
I get the response..Any Help please..I know this is naive but i am still learning and very new to this.Pardon if too naive.Thank you
in your query.js
export the functions in a object
module.exports = {
getAllUsers: getAllUsers,
getAllPref: getAllPref,
}

how to use the app helpers in node.js?

I want to add a script in app.js file here is the code:
app.helpers({
renderScriptsTags: function (all) {
if (all != undefined) {
return all.map(function(script) {
return '<script src="/javascript/' + script + '"></script>';
}).join('\n ');
}
else {
return '';
}
}
});
app.dynamicHelpers({
scripts: function(req, res) {
return ['canvasjs.min.js'];
}
});
while executing its give me an error shown below:
app.helpers({
^
TypeError: Object function (req, res, next) {
app.handle(req, res, next);
} has no method 'helpers'
at Object.<anonymous> (/var/www/html/nodeproject/helloworld/index.js:27:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
As your error is showing app does not have helper funtion. Memeber functions of app are listed here http://expressjs.com/en/api.html
if you want to use helpers you will have to create them explicity like
//helper.js
module.export = function(){
// Your logic here
};
//usage
var helper = require('./relative/path/to/helper');
var getValueFromHelper = helper();
hope it helps :)

Object function createServer() has no method 'bodyparser'

I am new to nodeJS. I am trying to use different middlewares with connect middleware.
this is my code:
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = connect()
.use(connect.bodyParser())
.use(connect.cookieParser('tobi is a cool ferret'))
.use(function(req, res){
console.log(req.cookies);
console.log(req.signedCookies);
res.end('hello\n');
}).listen(3000);
I have installed every middleware through npm.
I am getting this error while running this file.
/home/dipesh/Desktop/temp/temp.js:5
.use(connect.bodyParser())
^
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'bodyParser'
at Object.<anonymous> (/home/dipesh/Desktop/temp/temp.js:5:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
Any suggestions?
.use(bodyParser())
not
.use(connect.bodyParser())
You have required body-parser, but then never used it.
You are essentially doing
var a = function(){};
var b = {};
b.a();
which is not correct because b has not 'a' property.

Resources