简体   繁体   English

分配什么 res.json 返回到 Node.js 中的变量

[英]Assign what res.json return to variable in Node.js

I'm building a social network - and for that I use Node.js.我正在建立一个社交网络——为此我使用 Node.js。 I'm new to the subject, and this's my first post on the subject, I'll be happy if you understand me.我是这个主题的新手,这是我关于这个主题的第一篇文章,如果你能理解我,我会很高兴。

In my social network I want to use an algorithm that has it in the "npm" kmeans algorithm.在我的社交网络中,我想使用“npm”kmeans 算法中的算法。

I try to keep within a variable what the function returns to me and then continue to do calculations.我尝试将 function 返回给我的内容保持在一个变量内,然后继续进行计算。 I think the problem is very minor, but for a few hours I sit on it and can't figure it out.我认为这个问题非常小,但是我坐了几个小时却无法弄清楚。

I'm adding the code of what I've done so far:我正在添加到目前为止我所做的代码:

//kmeans.js file

const kmeans = require('kmeans-engine');

exports.addUserKmeansMatch = (req, res) => {
  const engineers = [
    // frontend engineers
    { html: 5, angular: 5, react: 3, css: 3 },
    { html: 4, react: 5, css: 4 },
    { html: 4, react: 5, vue: 4, css: 5 },
    { html: 3, angular: 3, react: 4, vue: 2, css: 3 },

    // backend engineers
    { nodejs: 5, python: 3, mongo: 5, mysql: 4, redis: 3 },
    { java: 5, php: 4, ruby: 5, mongo: 3, mysql: 5 },
    { python: 5, php: 4, ruby: 3, mongo: 5, mysql: 4, oracle: 4 },
    { java: 5, csharp: 3, oracle: 5, mysql: 5, mongo: 4 },

    // mobile engineers
    { objc: 3, swift: 5, xcode: 5, crashlytics: 3, firebase: 5, reactnative: 4 },
    { java: 4, swift: 5, androidstudio: 4 },
    { objc: 5, java: 4, swift: 3, androidstudio: 4, xcode: 4, firebase: 4 },
    { objc: 3, java: 5, swift: 3, xcode: 4, apteligent: 4 },

    // devops
    { docker: 5, kubernetes: 4, aws: 4, ansible: 3, linux: 4 },
    { docker: 4, marathon: 4, aws: 4, jenkins: 5 },
    { docker: 3, marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, nagios: 3 },
    { marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, linux: 3, puppet: 4, nagios: 5 }
  ];

  kmeans.clusterize(engineers, { k: 4, maxIterations: 5, debug: true }, (err, result) => {
    res.json(result.clusters)
    .then((data) => {
    let resultCluster = data; //<--- I want to perform a calculation on the object and then return it.
      res.json(resultCluster)
    })
    .catch((err) => {
      console.error(err);
      return res.status(500).json({ error: err.code });
    });
  })
};
//index.js file

const {
  addUserkMeansMatch
} = require('kmeans.js');

app.get('/kmeans', addUserkMeansMatch);
exports.api = functions.region('europe-west1').https.onRequest(app);

The problem is: I want to insert the information that the kmeans.clusterize function returns into the resultCluster object.问题是:我想将kmeans.clusterize function返回的信息插入resultCluster object。 I can not enter information into the resultCluster.我无法在 resultCluster 中输入信息。

Then I want to perform calculations with resultCluster and return what I will have in the calculations with resultCluster.然后我想用 resultCluster 执行计算,并返回我在 resultCluster 计算中的内容。

You're calling res.json() twice, and you're using promise syntax on it for no reason.您调用res.json()两次,并且无缘无故地使用 promise 语法。 It should be simply应该很简单

kmeans.clusterize(engineers, { k: 4, maxIterations: 5, debug: true }, (err, result) => {
  if (err) {
    console.error(err);
    res.status(500).json({ error: err.code });
  } else {
    const data = result.clusters; 
    … // perform a calculation on the object
    res.json(data);
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM