Is it possible to send the result to the browser without using result.send?

Although I can successfully query a MS-SQL server, I'm having trouble displaying the result in the browser.

var express = require('express');
const sql = require("mssql/msnodesqlv8");
var app = express();

const main = async() => {
  const pool = new sql.ConnectionPool({
    server: "example.com",
    database: "example",
    options: {
      trustedConnection: true
    }
  });

  await pool.connect();

  const request = new sql.Request(pool);
  const query0 = 'select getdate()' // Let's keep it simple..
  const result = await request.query(query0);

  //console.dir(result);
  //console.log(result.recordsets[0])
  return result.recordsets[0]
};

app.get('/', function(res, req) {
  var a = main()
  a.then(function(r) {
    console.log(r[0])
    res.send(r[0])
  }, function(err) {
    console.log('fetch error', err)
  }).catch(function(error) {
    console.log('really bad:', error)
  })
})
var server = app.listen(5000, function() {
  console.log('Server is running.....');
});

I am able to retrieve the date but encountering the following error:

Server is running..... { '': { 2018-07-17T11:30:02.060Z nanosecondsDelta: 0 } } really bad: TypeError: res.write is not a function at C:\node2mssql2\node2mssql\app0.js:34:13

Any thoughts on what might be causing this? Your help is much appreciated.

Answer №1

Consider making the following changes: app.get('/',function(res,req){

Update to: app.get('/',function(req,res){

Also, consider adding status(200) to res.status(200).send(r[0])

It's important to always return a response in every situation (error or success).

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

Two events in an arrow-guided grid including a text box (jQuery)

I have successfully implemented an editable table using jQuery where users can click on a cell to input text. The goal now is to enable the use of ARROW keys for quick navigation within the table. Below is my current code: $( function () { $( "td" ). ...

The final marker is consistently used by the click event

Currently exploring the Google Maps API for the first time and facing a challenge in Rails when trying to add a click event for each marker. I am looping through the team_locations model to extract latitude and longitude data in order to set up markers. ...

Dynamically insert a new row into an HTML table using AJAX and refresh the table with .load method

I am facing an issue with my HTML table that loads data dynamically through a PHP snippet containing SQL queries. There is a Select option and a button on the table to add a new row, which triggers an AJAX procedure to send the data to PHP for insertion in ...

I am in search of a JavaScript or jQuery plugin for an HTML slider with a unique range functionality

I've been searching online but couldn't find a slider like the one shown in the attachment. Can anyone help? Is there a way to create this specific slider or is there an existing plugin or library for it? Please refer to the image below :https:/ ...

Sending two objects back in res.send() in an API: A step-by-step guide

I've got an API that looks like this router.get('/exist', async (req, res) => { try { const { user: { _id: userId } } = req; const user = await User.findById(userId); const profile = await Profile.findById(user.profile, &apo ...

Angular component injected with stub service is returning incorrect value

While attempting to write tests for my Angular component that utilizes a service, I encountered an issue. Despite initializing my userServiceStub property isLoggedIn with true, the UserService property appears false when running the tests. I experimented ...

The process for deleting data using firebase-admin

Hey everyone, I'm currently working with the firebase-admin library which can be found here. I've been referring to the Firebase documentation available here, but unfortunately, I'm unable to locate the section on how to remove data using fi ...

What impact does incorporating a new test case have on code coverage in Jest?

I recently encountered an unexpected situation while working on a Jest test suite. Despite not making any changes to the codebase, I simply added a new test. Originally: mylibrary.js | 95.65 | 89.66 | 100 | 95.65 | 54-56,84 ------------ ...

Despite using callbacks when passing props in React JS components, the rerendering still occurs

In my React project, I implemented callbacks to prevent excessive rerendering due to the large amount of data being displayed and numerous buttons that trigger these rerenderings when clicked by the user. The useCallback hooks are effective in preventing ...

Enabling the acceptance of repeated values in the ui-select element

ui-select does not permit the insertion of duplicate values in a select list item. However, I have a scenario where a user needs to input a value multiple times that will not be selected from the dropdown list. Is there a way to accomplish this using the ...

Step-by-step guide on how to configure the URL path in an AJAX function call

How can I dynamically set the URL path in an AJAX function call when clicking on a page so that the page name is included in the URL? For example, if I click on a particular page (let's say the "ask" page on Stack Overflow), the URL should look like: ...

Tips for designing a pre-selection process

I've been considering the idea of implementing a temporary screen that allows users to choose between different options before proceeding to a specific page. For instance, on the contact page, users would be prompted with a question like "Are you loo ...

The close button is not functioning properly

I created a script to close my div element by clicking on the 'x' icon, but instead of deleting the div only, the whole page gets deleted. I'm not sure where I went wrong, can anyone help me? HTML <div class="note"> <span id ...

Unable to assign the value 'hello' to an undefined property in TypeScript

I'm attempting to define a class in TypeScript, but I keep encountering the error shown below. Here is the execution log where the error occurs: [LOG]: "adding" [LOG]: undefined [ERR]: Cannot set property 'hello' of undefined class Cust ...

How can I fetch data from SQL using JavaScript based on a specific value in PHP?

My application is built using the Yii2 framework. Within my application, there is a view.php file that consists of an element and a button. The element, <div id="userId">, contains the user's login ID, and I aim to use the button to re ...

Error: Unable to iterate over JSON data as json.forEach is not a valid function

let schoolData = { "name": "naam", "schools" : [ "silver stone" , "woodlands stone" , "patthar" ], "class" : 12 } schoolJSON = JSON.stringify(sc ...

Leverage JSON data within an AngularJS controller

I have a JSON list in Angular that looks like this: vm.allnews = actualNews; After checking it with console.log, I can see that it's working fine and I am able to retrieve all news from the array list. Each news item has a title which I can display ...

achieve precise outcomes using mapping techniques

I am currently learning react.js and encountering an issue with obtaining specific results on the map. Below is the code I am working with: render(){ const friends = [ {id:1, name: 'Dave',age:50}, {id:2,name: 'Kellie',age:42}, {id:3, ...

What is the best way to extract every nth digit from a number using a given reference point?

The subject of the title may cause confusion, so let me clarify my goal. With values of n = 51 and m = 24, I aim to achieve this result: [ {start:0, end:24}, {start:24, end:48}, {start:48, end:51} ] Currently, I have made progress on this ...

javascript date.js parsing and sorting an array of objects

I'm currently working on creating a JavaScript array of objects, utilizing date.js to parse dates, and then sorting that array using date.js compare methods. Below is a snippet of code that highlights the two issues I am facing. Issue 1: How can I ha ...