Sequelize - How can I return JSON objects of the database results only?
Though poorly documented, this does exist in Sequelize.
Couple Ways:
1. For any response object that resulted from a query, you can extract only the data you want by appending .get({plain:true})
the the response like so:
Item.findOrCreate({...})
.spread(function(item, created) {
console.log(item.get({
plain: true
})) // logs only the item data, if it was found or created
Also make sure you are using the spread
callback function for your type of dynamic query promise. And note that you have access to a boolean response, created
, which signifies whether or not a create query was executed.
2. Sequelize provides the raw
option. Simply add the option {raw:true}
and you will receive only the raw results. This will work on an array of results, the first method should not, as get
will not be a function of an array.
Sequelize find in JSON field
According to this link
You can approach the answer via 3 ways:
Nested Key
{
"meta.audio.length": {
[Op.gt]: 20
}
}
Nested object
{
meta: {
video: {
url: {
[Op.ne]: null
}
}
}
}
Containment
{
"meta": {
[Op.contains]: {
site: {
url: 'http://google.com'
}
}
}
}
Return data only without field name in Sequelize ORM
No, {"data"}
isn't a valid JSON structure. You could convert it to an array of "datas" using Array.map()
; passing raw: true
to findAll()
will return an array of plain JSON objects which is more efficient than converting to Model Instances first before mapping them to just the values you want.
const results = await Model.findAll({
attributes: ['value'],
where: {
code: { [Op.like]: 'images_default_droplet_%' },
is_published: 1
},
raw: true, // return array of plain JSON objects
});
// 'datas' is an array of ["data","data","data"...]
const datas = results.map((result) => result.value);
How to pass the output of a Sequelize query to another function that will return a JSON object with express?
I can't answer your last question about the format of your data because I'm unfamiliar with Sequelize, but the trouble you're having passing the data to your ExpressJS response stems from a lack of understanding of the Promise pattern. Have your service function return the Promise instead:
// services/orders.js
import { Order } from '../models'
const test = () => Order.findAll();
// export test at some point
And then call the service function inside your controller to access the returned data:
// controllers/orders.js
function getOrders(req, res) {
orderServices.test()
.then(data => res.send({ data }));
}
Look into asynchronous programming patterns- callbacks first, then Promises- and the reasoning behind why you must take this approach will become clear.
This answer should cover a considerable amount of the relevant subject matter.
Related Topics
Filereader Onload With Result and Parameter
Trying to Load a Script File from My Node Server - Get 404
Display Data Label (Legend) in Line-Chart Using Chartjs
How to Get a File() or Blob() from an Url in JavaScript
How to Set the Multi Sliders in One Page
The Onclick Is Not Working on First Click
Copy/Paste from Excel to a Web Page
How to Get Multiple Comma Separated Values from Url
React - Clearing an Input Value After Form Submit
Matching a Forward Slash With a Regex
How to Convert Postgresql Time Without Time Zone to Date in JavaScript
How to Prevent Drag on a Child, But Allow Drag on the Parent
How to Call the Same Method for Different Elements With Different Ids
Error: Permission Denied to Access Property "Document"
Javascript Odd and Even Separation in an Array of Numbers
Mutlple Owl Carousel in One Page With Different Setting
How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals