Comments (3)
- Ideally you should not be calling async functions or database model functions inside your view - it would be better to have a function somewhere before this step which obtains all necessary data.
- ORM model instances behave like JSON objects:
console.log(modelInstance)
// prints weird get/set stuff
console.log(JSON.stringify(modelInstance, null, 2))
// prints the actual data
The console thing makes it look confusing, but it behaves like a JSON object, so it should just work.
from node-orm2.
@dxg I really can't get this working. Here's my code:
let devices = [];
req.models.cluster.all(function(err, clusters) {
clusters.forEach((cluster) => {
cluster.getDevices((err, device) => {
devices.push(JSON.stringify(device));
})
console.log(devices);
});
});
console.log(devices) returns []
from node-orm2.
@GHOSTBG it's because cluster.getDevices
is asynchronous and will return some time later. The console.log
is printing before the call completes.
I recommend using a package like https://www.npmjs.com/package/async as then you can change the code to:
const async = require('async');
const _ = require('lodash');
function getDevices (req, cb) {
req.models.cluster.all(function(err, clusters) {
if (err) return cb(err);
async.map(clusters, function (item, cb) {
return item.getDevices(cb);
}, function (err, devices) {
if (err) return cb(err);
console.log(JSON.stringify(devices, null, 2));
cb(null, _.flatten(devices));
});
});
}
// somewhere else
getDevices(req, function (err, devices) {
// we have devices, or an error
});
I highly recommend using async+await instead of callbacks, as then you can do something like this:
const _ = require('lodash');
const Promise = require('bluebird');
const getDevices = async function () {
const clusters = await req.models.cluster.allAsync();
const devices = _.flatten( await Promise.map(clusters, c => c.getDevicesAsync()) );
console.log(JSON.stringify(devices, null, 2));
return devices;
}
// Somewhere in your code:
const devices = await getDevices();
from node-orm2.
Related Issues (20)
- About timezone setting in sqlite HOT 1
- user or conditions HOT 1
- Adding a column to a table? HOT 2
- Missing primary key value after create
- Pool connection with postgres HOT 2
- only in findAsync options doesn't seem to do anything HOT 2
- OtherInstance.save is not a function [hasOne Association]
- Validations not working? HOT 1
- Cannot read property 'mapsTo' of undefined HOT 3
- groupBy is not working with findAsync HOT 1
- Something went wrong?
- Group by Month?
- Callback not working !
- Stored procedure and custom sql statement support for MySql HOT 1
- Need to update node-sql-ddl-sync once lodash is updated there HOT 1
- ORM Support for MySQL v8.x HOT 1
- IF condition
- findBySmth has doubles
- How to change to a synchronous query? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-orm2.