0

I have a NodeJS API with Express. In my views, I need to call SQL Server functions like in the following example.

const express = require('express');
const router = express.Router();
const { poolPromise } = require('../dbconfig');

router.use(express.json());

router.post('/addDesc', async(req, res) => {
    try {
        const
            username= req.body.nomUsager,
            descr = req.body.description ;

        const pool = await poolPromise;
        const result = await pool.request().query(`Select dbo.AddDescr('${nomUsager}', '${descr}')`);

        console.log(result.recordset[0]);

        return res.json({
            // I want to return the value returned by me SQL Functions, but the previous console.log() returned {"": "The function value returned"}. Is it possible to return only the value like that : { "the function value returned" }
        });

    } catch (err) {
        return res.json({
            "Fail"
        });
    }
});

module.exports = router

It works fine, however, how do I get only the result returned by my function rather than an object with an unnamed property? (See my console.log ())

I would like not to use an alias, all my sql function and stored procedure is done

1 Answer 1

1

I am not totally sure if I understand the question but you can get just the string by using:

result.recordset[0][""];.

Using the square brackets with quotes: [""] will help you get the value. However, JavaScript objects are {name: value} pairs so you cannot return a value alone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.