The function 'get_categories' cannot be accessed due to its undefined state

I've been working on integrating a node module from Semantics3 into my project. The code I've written is stored in a file called search.js within the routes folder. However, I'm encountering an issue where I am getting an undefined-method error when calling sem3.categories.get_categories. Strangely enough, the same code functions correctly when placed in the app.js file. Could it be that I am referencing the node module incorrectly? If so, what is the proper way to reference a module from a file other than app.js?

var express = require('express');
var router = express.Router();
var api_key = 'my key';
var api_secret = 'my secret';
var sem3 = require('semantics3-node')(api_key,api_secret);

/* GET search results. */
router.get('/', function(req, res) {
    // Build the query
sem3.products.categories_field( "cat_id", 4992 );

// Make the query
sem3.categories.get_categories(
   function(err, categories) {
      if (err) {
         console.log("Couldn't execute query: get_categories");
         return;
      }
    // View the results of the query
    console.log( "Results of query:\n" + JSON.stringify( categories ) );
    res.send(JSON.stringify(categories));

   }
);




});

module.exports = router;

Answer №1

If you're having trouble with Node.js and require statements, this blog post might shed some light on the issue:

While I haven't worked with Node recently, it seems like the problem could be related to relative or absolute paths. It's possible that Node is looking for a node_modules folder within your 'routes' subdirectory. To fix this, you may need to specify the exact relative directory path from 'routes' to node_modules.

Instead of using

var sem3 = require('semantics3-node')(api_key, api_secret);

You might want to try something like

var sem3 = require('[insert relative path here e.g. ../modules/lib/semantics3-node]')(api_key,api_secret);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to storing data in the browser's local storage with a Node.js Express backend

Up until this point, I relied on cookies to store preferences for my websites. However, as time has passed, the data stored in these cookies has grown too large and now exceeds the limit of 4096 characters. Therefore, I need to explore an alternative meth ...

Guide on swapping OAuth2 authorization codes for access tokens sent by a client (Ember) through Express

Currently, I am utilizing ember-simple-auth and torii within my Ember application to manage OAuth2 authentication with Facebook and Google on the client side. As a result of this process, I obtain an authorization code. My objective is to transmit this co ...

Node.js worker_threads: How to determine when all workers have finished their tasks

Recently dove into learning about worker_threads in nodejs When all the work is done, the final worker should complete the script process.exit(); my index.js const { Worker } = require("worker_threads"); const logUpdate = require("log-update"); ...

Function that returns an array

Hey there, wondering about variable scope in closures! I've come across a lot of questions on this topic but haven't found the solution to my issue. Here's the code snippet: var teams = []; var players = []; var getRoles = function(roleL ...

What causes the slash URL to behave differently than other URLs when running through the middleware of NodeJS?

When I type http://localhost:3000/product into the browser, why do I see output for both '/' and '/product'? Take a look at this code snippet below. const express = require('express'); const app = express(); // http://loca ...

What is the best way to deliver HTML and JavaScript content using Node.js from virtual memory?

Situation I'm currently developing an in-browser HTML/JS editor, utilizing memory-fs (virtual memory) with Webpack and webpack-html-plugin to package the files created by users in the editor. Storing the files in virtual memory helps avoid I/O operat ...

What is the best way to perform a callback after a redirect in expressjs?

After using res.redirect('/pageOne') to redirect to a different page, I want to call a function. However, when I tried calling the function immediately after the redirect like this: res.redirect('/pageOne'); callBack(); I noticed th ...

Fetch a document from a NodeJS Server utilizing Express

Is there a way to download a file from my server to my machine by accessing a page on a nodeJS server? I am currently using ExpressJS and I have attempted the following: app.get('/download', function(req, res){ var file = fs.readFileSync(__d ...

Everytime I try to initiate npm start on VS Code, I encounter an error

(node:6852) [LRU_CACHE_OPTION_maxAge] DeprecationWarning: The maxAge option is deprecated. please use options.ttl instead (Use `node --trace-deprecation ...` to show where the warning was created) > <a href="/cdn-cgi/l/email-protection" class="__cf_ ...

failure to properly assign a property during model update in mongoose

My BaseSchema contains logic that should set values for two properties when a new Model is created: schema.pre("save", function (next) { if (!schema.isNew) { this.createDate = new Date(); this.createBy = "kianoush"; } next(); }); If updating, ...

Pass on Redis Pub/Sub messages to a designated client in socket.io through an Express server

Currently, in my express server setup, I am utilizing socket.io and Redis pubsub. The process involves the server subscribing to a Redis message channel and then forwarding any incoming Redis messages to a specific WebSocket client whenever a new message i ...

Issue encountered while trying to install node-sass due to permissions restriction

I encountered an issue while trying to add node-sass to my create react app. The command I used was: sudo npm install node-sass --save However, it resulted in the following error: Unable to save binary /Users/username/Desktop/code/advocado/node_modu ...

Error: Const usage in strict mode within GitHub and Codeship is causing a SyntaxError

While executing grunt test in my source code on Github/Codeship, I encountered the following error. The setup command within Codeship is configured as follows: nvm install 0.12.6 nvm use 0.12.6 npm install grunt-cli bower -g npm install bower install -p ...

The busser completed their task before completely going through all the information

I am currently working with an express application that utilizes busboy to parse form data. Although the function I have set up returns the field values of the form, it does not fully parse all the fields before the return call. module.exports = async fun ...

Having trouble getting MongoDB to connect correctly to my server (using node.js and express), any help would be greatly appreciated

Whenever I make an API request using Hoppscotch, I encounter a terminal error along with status code 500 on Hoppscotch. Despite my efforts to fix the issue, it persists. Below is the code snippet that I am currently working with: var mongodb = require(&apo ...

Error: Firebase Cloud Functions reference issue with FCM

Error Encountered An error occurred with the message: ReferenceError: functions is not defined at Object. (C:\Users\CROWDE~1\AppData\Local\Temp\fbfn_9612Si4u8URDRCrr\index.js:5:21) at Module._compile (modul ...

Displaying only the initial entry in a MongoDB collection through Bootstrap modals

I am currently using NodeJS, Handlebars, and Bootstrap to develop a basic web application. The goal is to iterate through a MongoDB collection of simulated products and exhibit their respective fields. The data is being presented in "product cards" (see i ...

Ways to duplicate Mongo DB schema using URI (replicate schema exclusively, exclude any data)

In short: I need to replicate all classes along with their columns (names and types), but without any data, from a specified URI. Here's an example of the URI format: mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?op ...

Passport: user information is not saved in the session

Recently, I delved into the world of nodejs and decided to create an authentication system. Following a tutorial, I successfully implemented it. To retrieve the user object after authentication, it is necessary to store it in the session. This was achieved ...

Transmitting command-line arguments while utilizing node-windows for service creation

Recently, I developed some custom middleware in Node.js for a client that functions well in user space. However, I am now looking to turn it into a service. To achieve this, I utilized node-windows, which has been effective so far. The only issue is that ...