I'm new in Node and I've watched some of the tutorials from the Net. My main problem is how I can show the result from the route I created. Well, I'm getting the correct data using the console but when I access it through browser it doesn't show the json encoded data.
Here is my product.js:
const express = require('express');
const router = express.Router();
const connection = require('../connection');
router.get('/',(req,res)=>{
res.send(products());
})
async function products(){
// console.log(products)
return await getProducts();
}
function getProducts(){
return new Promise((resolve,reject) =>{
connection.query('SELECT brand,description from products limit 100', function (err, rows, fields) {
if (err) throw err
resolve(JSON.stringify(rows))
})
})
}
module.exports = router;
Here is the console log result : http://prntscr.com/llxgk2
Here is the result from the postman: http://prntscr.com/llxgy3
;afterthrow err,resolve(JSON.stringify(rows)), and yourquerycall. Automatic Semicolon Insertion will handle it for you, but you don't appear to be intentionally relying on ASI (some people do, I don't recommend it), so I thought I'd mention it.async function products()is pretty much pointless as it does nothing but forwarding a function call. You could write that function asfunction products(){ return getProducts() }and the behaviour would be the same. Or you just usegetProducts()