Unforeseen SyntaxError: Unexpected symbol detected

Encountering an issue while attempting to send raw data as parameters in express. Specifically, there is an error occurring at the 'fields' variable...

function getWithQuery(req,res){
    console.log(req.params);
    var query = {name: new RegExp(name, 'i')};
    var fields = {"_id","name"};//i tried {_id, name}; and {'_id', 'name'}
    var maxRecs = 10;
    var sort = {name};
    dataService.getWithQuery(query, fields, maxRecs, sort)
    .then(function(data){
        if (data){
            res.send(data);
        }else {
            res.sendStatus(404).send("Doc dont exists");
        }
    })
    .catch(function (err){
        console.log("doc dont exists" + err);
        res.status(500).send(err);
    });
}

The following error is being thrown...

var  fields = {"_id","name"};
                    ^

SyntaxError: Unexpected token


Please provide suggestions on how to address this issue...thank you in advance

Answer №1

The issue stems from incorrect syntax usage: if you are looking for an object, make sure to include a : between key and value.

var fields = {_id: name};

If you intend to create an array, remember to use [] instead of {}:

var fields = ["_id", "name"];

In addition, there might be an error with var sort = {name} syntax as well.

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

Automatically submit form in Javascript upon detecting a specific string in textarea

Just getting started with JS and I have a question that's been bugging me. I have a simple form set up like this: <form method="POST" action="foo.php"> <textarea name="inputBox123"></textarea> <input type="submit" value="Go" name ...

Trouble accessing nested components in Angular CLI beyond the first level of components

I'm diving into Angular CLI for the first time and trying to recreate a previous web project of mine. I've managed to nest and display components inside the root component successfully, but after that, I'm facing issues referencing any compo ...

Issues persist with redirection when using AJAX and PHP in a login form, resulting in the user staying on the index page

After filling out both the email and password form fields and clicking the sign-in button, my script is functioning correctly. However, upon clicking the sign-in button, I want the user to be redirected to the home page. Currently, the user remains on the ...

Handling Errors in Node.js Applications

Good evening everyone! Today, I delved into experimenting with NodeJS and stumbled upon an issue related to error handling that I could use some assistance with. My scenario involves a database table where the playerName must be unique. Instead of blindly ...

Exploring the capabilities of Node.js for handling POST requests and

I'm currently modifying an application that triggers a specific task whenever a particular pin is utilized. Here's the scenario: my app sends a pin to the server via a POST command. What I aim for is my Node.js application detecting this pin and ...

Explain the concept of user roles using node_acl in combination with mongoose and express

I'm currently utilizing node_acl to handle authorization in my application. However, I'm unsure about how to implement roles for each user. app.js const mongoose = require('mongoose'); const app = express(); const security = require(. ...

What is the process of retrieving an image file in a Java post API when it is being transmitted as form data through Jquery?

I have encountered an issue with fetching file data in my POST API when utilizing three input file fields in JavaScript. The values are being sent using formdata in jQuery upon clicking the submit button, but I am experiencing difficulties in retrieving th ...

Showing events from MySQL database on Vue.js Fullcalendar

I am trying to fetch events from my MySQL database and pass them to my Vue component to be displayed on the FullCalendar. However, the event array is being populated with a full HTML document. Below is my EventController: public function getEvents() { ...

Having trouble reaching the unidentified function

There are two different scenarios where the 3rd party library (BrowserPrint.js) is used; FUNCTIONAL ENV - JS and jQuery with the 3rd party libraries included simply in the <head> section of the document and the main function being called in $(do ...

The POST request functions flawlessly on the local server, but encounters issues once deployed to Google Cloud Platform

Even though the Axios post request works fine on my local server, it throws a 404 not found error after I deploy the project on Google Cloud. On localhost, all requests are directed to URLs starting with http://localhost:9000/api/..., handling post reques ...

How can you display an alert message when new data is successfully added to a database using ajax?

In my web application, I have implemented a functionality to display an alert message when new data is successfully inserted into the database. The Ajax code below is responsible for sending a request to the EditDeleteLecture.php file. However, a challenge ...

Guide on hosting two html pages on a NodeJS server

Currently, I am in the process of learning NodeJS and Javascript with a goal to construct a basic server that can host 2 HTML pages. One page should be accessible via localhost:3000/index, while the other can be reached through localhost:3000/about. While ...

How can we use SWR to fetch user data conditionally based on their logged-in state?

I am facing an issue with setting the UI state based on whether a user is logged in or not. The UI should display different states accordingly. I am currently using SSG for page generation and SWR for user data retrieval. However, I noticed that when call ...

Protractor quickly launches and closes the Chrome browser without completing the entire scenario

In order to test my application using protractor, I created a scenario. The application begins with a non-angular login page and then progresses to an angular page after logging in. Here is the javascript code snippet that was utilized: var chai = requir ...

My content is being obstructed by a single-page navigation system

I attempted to create a simplified version of the issue I am facing. Basically, I am working on a header with navigation that stays at the top of the page while scrolling. The problem arises when clicking on a section in the navigation. The screen scrolls ...

Tips for testing Sequelize with Jasmine

In my database/index.ts file, I set up a Sequelize database using the code below: import { Sequelize } from 'sequelize'; const { DATABASE_DIALECT, DATABASE_HOST, DATABASE_PORT, DATABASE_USER_NAME, DATABASE_USER_PASSWORD, DATABASE_NAM ...

Mastering the art of using res.send() in Node.js

I have been working on a project using the mongoose API with Node.js/Express. There seems to be an issue with my get request on the client side as the data is not coming through. Can someone help me figure out what's wrong? Snippet of backend app.ge ...

AWS lambda not properly displaying static content for an Angular application when running Node Express server

Recently, I encountered an issue while trying to deploy my Angular application on AWS Lambda. I kept receiving a 403 exception for my static content, even though I used Express.js to configure the server. You can check out the problems I'm facing at t ...

Exploring dependencies in Node.js with depcheck to uncover instances of require('x') where 'x' is not listed in the package.json file

Recently, I came across a library called depcheck: https://www.npmjs.com/package/depcheck Initially, I assumed that it would solve my problem of finding modules used in the code but not declared in package.json. However, to my disappointment, it only loo ...

Node.js middleware for verifying required parameters

In my current application, I am utilizing node.js and express. I have developed multiple middleware functions, each one created in a similar fashion: function loadUser(req, res, next){ ... } I am interested in creating a middleware that can validate th ...