ERROR: Cannot read property 'name' of undefined when I use ajaxify.go on /categories


  • GNU/Linux

    The problem is that when I write the path " /categories" ,a function "ajaxify.go = function (url, callback, quiet)" is called and I obtain the error. My purpose is overwrite the path" /categories" with my code:

    This is my code:

    {
    			"hook": "static:app.preload", "method": "preload"
    		},
    

    This is my library.js:

    converter.preload = function(params, callback) {
    	var app = params.app,
    		hostMiddleware = params.middleware;
    		
    	helpers.setupPageRoute(app,'/categories', hostMiddleware,[], controllers.renderCategories);
    	helpers.setupPageRoute(app,'/api/categories', hostMiddleware,[], controllers.renderCategories);
    	callback();
    };
    

    This is my controllers.js:

    Controllers.renderCategories1 = function(req, res, callback) {	
    	res.locals.metaTags = [{
    		name: "title",
    		content: validator.escape(String(meta.config.title || 'NodeBB'))
    	}, {
    		name: "description",
    		content: validator.escape(String(meta.config.description || ''))
    	}, {
    		property: 'og:title',
    		content: '[[pages:categories]]'
    	}, {
    		property: 'og:type',
    		content: 'website'
    	}];
    		var ogImage = meta.config['og:image'] || meta.config['brand:logo'] || '';
    	if (ogImage) {
    		if (!ogImage.startsWith('http')) {
    			ogImage = nconf.get('url') + ogImage;
    		}
    		res.locals.metaTags.push({
    			property: 'og:image',
    			content: ogImage
    		});
    	}
    
    	var categoryData;
    	async.waterfall([
    		function(next) {
    			categories.getCategoriesByPrivilege('cid:0:children', req.uid, 'find', next);
    		},
    		function(_categoryData, next) {
    			categoryData = _categoryData;
    
    			var allCategories = [];
    			categories.flattenCategories(allCategories, categoryData);
    
    			categories.getRecentTopicReplies(allCategories, req.uid, next);
    		}
    	], function(err) {
    		if (err) {
    			return next(err);
    		}
    
    		var data = {
    			title: '[[pages:categories]]',
    			categories: categoryData
    		};
    		//console.log("TITE "+data.title);
    		if (req.path.startsWith('/api/categories') || req.path.startsWith('/categories')) {
    			data.breadcrumbs = helpers.buildBreadcrumbs([{
    				text: data.title
    			}]);
    		}
    
    		data.categories.forEach(function(category) {
    			if (category && Array.isArray(category.posts) && category.posts.length) {
    				category.teaser = {
    					url: nconf.get('relative_path') + '/topic/' + category.posts[0].topic.slug + '/' + category.posts[0].index,
    					timestampISO: category.posts[0].timestampISO,
    					pid: category.posts[0].pid
    				};
    			}
    		});		
    		
    		res.render('categories', data);
    		
    	});
    };
    
    

    This is ajaxify.js and the error is at line " app.template=data.template.name"

    ajaxify.go = function (url, callback, quiet) {
    		if (!socket.connected) {
    			if (ajaxify.reconnectAction) {
    				$(window).off('action:reconnected', ajaxify.reconnectAction);
    			}
    			ajaxify.reconnectAction = function(e) {
    				ajaxify.go(url, callback, quiet);
    				$(window).off(e);
    			};
    			$(window).on('action:reconnected', ajaxify.reconnectAction);
    		}
    
    		if (ajaxify.handleRedirects(url)) {
    			return true;
    		}
    
    		app.leaveCurrentRoom();
    
    		$(window).off('scroll');
    
    		if ($('#content').hasClass('ajaxifying') && apiXHR) {
    			apiXHR.abort();
    		}
    
    		if (!window.location.pathname.match(/\/(403|404)$/g)) {
    			app.previousUrl = window.location.href;
    		}
    
    		url = ajaxify.start(url);
    
    		// If any listeners alter url and set it to an empty string, abort the ajaxification
    		if (url === null) {
    			$(window).trigger('action:ajaxify.end', {url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title});
    			return false;
    		}
    		console.log("UR "+JSON.stringify(url));
    		previousBodyClass = ajaxify.data.bodyClass;
    		$('#footer, #content').removeClass('hide').addClass('ajaxifying');
    
    		ajaxify.loadData(url, function(err, data) {
    
    			if (!err || (err && err.data && (parseInt(err.data.status, 10) !== 302 && parseInt(err.data.status, 10) !== 308))) {
    				ajaxify.updateHistory(url, quiet);
    			}
    
    			if (err) {
    				return onAjaxError(err, url, callback, quiet);
    			}
    
    			retry = true;
    			//console.log(data);
    			app.template = data.template.name;
    			console.log("DATA "+JSON.stringify(data));
    			require(['translator'], function(translator) {
    				translator.load(config.defaultLang, data.template.name);
    				renderTemplate(url, data.template.name, data, callback);
    			});
    		});
    
    		return true;
    	};
    

    when the path go on the "/categories" and the method ajaxify.go is called (ajaxify.loadData()) , it returns the HTML code of page and not an object and so it shows me this error.
    Anyone can help me to resolve this?


  • Admin

    Is there any particular reason you want to override /categories? It might just be easier to set up a new route under a different name.


  • GNU/Linux

    @julian I need to override "/categories". I don't know how is it possible that when the

    "ajaxify.loadData(url, function(err, data) {"

    is called, with url="categories", the response is an HTML page and not a object


  • Admin

    @doppy said in ERROR: Cannot read property 'name' of undefined when I use ajaxify.go on /categories:

    helpers.setupPageRoute(app,'/api/categories', hostMiddleware,[], controllers.renderCategories);

    You don't need this, setupPageRoute already creates the api route. https://github.com/NodeBB/NodeBB/blob/master/src/routes/helpers.js#L9

    I am not sure if that will fix your problem though. To confirm everything is correct you can go to /api/categories and you should get back json data instead of html.


  • GNU/Linux

    @baris I need override the path because I need modify some values, so the default path is not correct for me, My question is how can I override the default "/api/categories" path and modify some value?


  • Admin

    You'll want to listen to the filter:categories.build hook. You can remove all of the code creating the route override.


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.