Node.js 将变量从nodejs mongodb传递到其他js文件中进行计算,然后将该值导出到同一app.js

Node.js 将变量从nodejs mongodb传递到其他js文件中进行计算,然后将该值导出到同一app.js,node.js,mongodb,Node.js,Mongodb,我需要将mondodb数据库中特定Id中的变量传递到calculation.js,然后进行一些计算,并再次将结果传递到app.js文件以进行渲染 我想在calculation.js中使用的变量是 var cash = foundAccount.cash; var expenses = foundAccount.expenses; calculation.js文件示例: var calc = cash - expenses; 然后将“calc”的值导出到app.

我需要将mondodb数据库中特定Id中的变量传递到
calculation.js
,然后进行一些计算,并再次将结果传递到
app.js
文件以进行渲染

我想在
calculation.js
中使用的变量是

        var cash = foundAccount.cash;
        var expenses = foundAccount.expenses;
calculation.js
文件示例:

var calc = cash - expenses;
然后将“
calc
”的值导出到
app.js
res.render

app.get("/account/:id/balance", function(req, res){
    //find the account with provided ID
    account.findById(req.params.id, function(err, foundAccount){
        if(err){
            console.log(err);
        } else {
    //render balance template with that account
            var cash = foundAccount.cash;
            var expenses = foundAccount.expenses;
            res.render("balance", {account: foundAccount, calc: calc});
        }
    });
谢谢你的帮助

问候


Alan

Calc
可以是
calculation.js
中的函数,例如:

function calc(cash, expenses) {
  return cash - expenses
}

export default calc;
然后,您可以将该函数导入您的
app.js
文件,例如:

import calc from './calculation';
然后打电话:

res.render("balance", {account: foundAccount, calc: calc(foundAccount.cash, foundAccount.expenses)});

Calc
可以是
calculation.js
中的函数,例如:

function calc(cash, expenses) {
  return cash - expenses
}

export default calc;
然后,您可以将该函数导入您的
app.js
文件,例如:

import calc from './calculation';
然后打电话:

res.render("balance", {account: foundAccount, calc: calc(foundAccount.cash, foundAccount.expenses)});

因为你没有使用ES6

您可以从路由文件顶部的
calculation.js
导入
calc
函数:

var calculations = require('/path/to/calculations');

app.get("/account/:id/balance", function(req, res){
//find the account with provided ID
account.findById(req.params.id, function(err, foundAccount){
    if(err){
        console.log(err);
    } else {
//render balance template with that account
        var calc = calculations.calc(foundAccount); //You call the imported function
        res.render("balance", {account: foundAccount,  calc});
    }
});
您必须在
calculation.js
中导出此函数:

  class Calculations {
     calc(account) {
        var cash     = account.cash; 
        var expenses = account.expenses;
        var calc    =  // do your calculations
        return calc;
     }
  }
  module.exports = Calculations;
[编辑]由于您提到,您使用es6,因此您应该开始使用
let
const
,而不是
var
,您可以在此处阅读更多有关好处的信息。 现在代码应该是这样的:

import {calc} from '/path/to/calculations';

 app.get("/account/:id/balance", function(req, res){
 //find the account with provided ID
 account.findById(req.params.id, function(err, foundAccount){
     if(err){
    console.log(err);
     } else {
 //render balance template with that account
         const calc = calc(foundAccount); // You call the imported function
         res.render("balance", {account: foundAccount, calc});
     }
 });
calculations.js
中:

export const calc = (account) => {
    const cash     = account.cash; 
    const expenses = account.expenses;
    const calc     =  // do your calculations
    return calc;
};

因为你没有使用ES6

您可以从路由文件顶部的
calculation.js
导入
calc
函数:

var calculations = require('/path/to/calculations');

app.get("/account/:id/balance", function(req, res){
//find the account with provided ID
account.findById(req.params.id, function(err, foundAccount){
    if(err){
        console.log(err);
    } else {
//render balance template with that account
        var calc = calculations.calc(foundAccount); //You call the imported function
        res.render("balance", {account: foundAccount,  calc});
    }
});
您必须在
calculation.js
中导出此函数:

  class Calculations {
     calc(account) {
        var cash     = account.cash; 
        var expenses = account.expenses;
        var calc    =  // do your calculations
        return calc;
     }
  }
  module.exports = Calculations;
[编辑]由于您提到,您使用es6,因此您应该开始使用
let
const
,而不是
var
,您可以在此处阅读更多有关好处的信息。 现在代码应该是这样的:

import {calc} from '/path/to/calculations';

 app.get("/account/:id/balance", function(req, res){
 //find the account with provided ID
 account.findById(req.params.id, function(err, foundAccount){
     if(err){
    console.log(err);
     } else {
 //render balance template with that account
         const calc = calc(foundAccount); // You call the imported function
         res.render("balance", {account: foundAccount, calc});
     }
 });
calculations.js
中:

export const calc = (account) => {
    const cash     = account.cash; 
    const expenses = account.expenses;
    const calc     =  // do your calculations
    return calc;
};

我正在使用ES6。我正在使用ES6。谢谢你的回复。我现在收到加州大学的NaN。知道吗?如果
calc
返回
NaN
一个或两个输入变量在函数调用时是
未定义的
。检查那些值谢谢你的回复。我现在收到加州大学的NaN。知道吗?如果
calc
返回
NaN
一个或两个输入变量在函数调用时是
未定义的
。检查这些值