Coder Social home page Coder Social logo

Comments (3)

dxg avatar dxg commented on September 5, 2024
  1. 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.
  2. 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.

DimitarDechew avatar DimitarDechew commented on September 5, 2024

@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.

dxg avatar dxg commented on September 5, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.