module.exports内にある別の関数 'NextMessageCalculation'内の別のローカル関数 'MessageScoring'私はスコープを元に戻すことに問題があります。この関数は他のローカル関数を正常に呼び出しますが、スコープは変更され、前のスコープの変数はすべてundefinedを返します。どのようにこれを修正するか、これを避けるためにローカル関数を呼び出すより良い方法がありますか?Node.JS内のmodule.exports内で "local"関数を呼び出した後にスコープを元の関数に戻す方法
module.exports = {
MessageScoring: function(){
var attrs_list = Object.keys(message_attr_obj);
var score = 0;
for (i in attrs_list){
if(user_data[attrs_list[i]]){
score = score + user_data[attrs_list[i]]["beta"];
}
else{
//user has no response with that attr yet so score is not added
}
}
score = score/attrs_list.length;
return score}... //returns an int
NextMessageCalculation: function(){ //has a call to a mongodb and the logic takes place in the callback
MESSAGE_collection.find({'message_set':user_data.message_set,'id':{$nin:[1111]}}).toArray(function(err,doc){
if (doc.length === 0)
{
USERDATA_collection.update({phone:phone_num},{$set:{send_message:false}},function(err,res) {
db.close()
});
}
else{
var highest_score = "";
var next_message = "";
for (i in doc)
{
console.log(doc[i]['id']);
var score = module.exports.MessageScoring(doc[i].attr_list,user_data);
console.log(doc[i]['id']); <---- becomes undefined here after calling scoring function which correctly returns int
if (highest_score === "")
{
console.log(doc[i]);
next_message = doc[i].id;
highest_score = score;
}
if (highest_score < score)
{
next_message = doc[i].id
highest_score = score;
}
console.log(doc[i].id);
}
残りのコードは提供できますか? MessageScoringの動作、d、i、およびdoc変数の定義場所が不明です。 – djfdev