Pass NodeJS callback from exported function to view

dalton

I'm developing a NodeJS app and have a function being exported like so:

module.exports = {
  userCount: function(done) {
    user.count(function(err, count) {
      done(count);
    });
  }
}

What I'm trying to do is pass the callback to a view. This is how I'm currently doing it:

methods.userCount(function(count) {
  app.get("/", function(req, res) {
    res.render("../views/index", {
      userCount: count
    });
  });
});

That works fine but my problem is there will be multiple functions being exported, therefore, I will need to pass multiple callbacks to the same view.

Is the way I'm currently doing it the best/only way to pass the callback to the view?

The only other way I thought about doing it was to declare a variable and just add the callback to the variable. Something like this:

var num;
methods.userCount(function(count) {
  num = count;
});

res.render("../views/index", {
  userCount: num
});

But I'm not sure if that's good practice. Is there a better way to do this?

Maroshii

You should call the userCount function inside the route handler not the other way around:

app.get("/", function(req, res) {
  methods.userCount(function(count) {
    res.render("../views/index", {
      userCount: count
    });
  });
});

How you calculate the count is an implementation detail that should be inside the userCount method.

I believe a better approach would be to run the userCount method as a route middleware and attach the count to res.locals:

app.use(function(req,res,next) {
  // error handling omitted for simplicity
  methods.userCount(function(count) { 
    res.locals.count = count;
    next();
  });
})

app.get("/", function(req, res) {
  res.render("../views/index", {
    userCount: res.locals.count
  });
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SwiftUI MVVM how to set callback from ViewModel function to View

分類Dev

Pass variable to main function Nodejs

分類Dev

How to pass user data to a callback function

分類Dev

Matlab pass variable from one callback to another

分類Dev

Update Ionic view from async callback

分類Dev

No exported member 'view'

分類Dev

Writing a function in nodejs, and having it display in the view

分類Dev

How to pass additional parameters to an encapsulated callback function in node.js

分類Dev

Return value from a function callback in a for loop

分類Dev

Setting variable to a return from a callback function

分類Dev

No result from callback function in Node.js

分類Dev

Pass the same View to a function that you are using in the setContentView()

分類Dev

How to pass data from one view to the next

分類Dev

Pass complex object from view to Controller

分類Dev

How to pass a string with slashes from view to template?

分類Dev

Pass Variable from View to Form Django

分類Dev

Pass @Published properties from view controllers to SwiftUI

分類Dev

How to pass GUID from View to JavaScript?

分類Dev

How to pass dictionary from controller to view?

分類Dev

How to Pass Details Model from Controller to View

分類Dev

Django how to pass value from template to view

分類Dev

How to pass argument from template to view in Flask

分類Dev

JQuery pass a variable from function to function

分類Dev

How to pass an array from a view to another and update the view accordingly

分類Dev

Is it possible to pass a callback to a webworker?

分類Dev

Pass variable to template callback

分類Dev

Error thrown from a mongoose-promise callback function is not caught

分類Dev

Execute coroutine from `call_soon` callback function

分類Dev

Emitting a Node.js Event from a C++ callback function

Related 関連記事

  1. 1

    SwiftUI MVVM how to set callback from ViewModel function to View

  2. 2

    Pass variable to main function Nodejs

  3. 3

    How to pass user data to a callback function

  4. 4

    Matlab pass variable from one callback to another

  5. 5

    Update Ionic view from async callback

  6. 6

    No exported member 'view'

  7. 7

    Writing a function in nodejs, and having it display in the view

  8. 8

    How to pass additional parameters to an encapsulated callback function in node.js

  9. 9

    Return value from a function callback in a for loop

  10. 10

    Setting variable to a return from a callback function

  11. 11

    No result from callback function in Node.js

  12. 12

    Pass the same View to a function that you are using in the setContentView()

  13. 13

    How to pass data from one view to the next

  14. 14

    Pass complex object from view to Controller

  15. 15

    How to pass a string with slashes from view to template?

  16. 16

    Pass Variable from View to Form Django

  17. 17

    Pass @Published properties from view controllers to SwiftUI

  18. 18

    How to pass GUID from View to JavaScript?

  19. 19

    How to pass dictionary from controller to view?

  20. 20

    How to Pass Details Model from Controller to View

  21. 21

    Django how to pass value from template to view

  22. 22

    How to pass argument from template to view in Flask

  23. 23

    JQuery pass a variable from function to function

  24. 24

    How to pass an array from a view to another and update the view accordingly

  25. 25

    Is it possible to pass a callback to a webworker?

  26. 26

    Pass variable to template callback

  27. 27

    Error thrown from a mongoose-promise callback function is not caught

  28. 28

    Execute coroutine from `call_soon` callback function

  29. 29

    Emitting a Node.js Event from a C++ callback function

ホットタグ

アーカイブ