mongoose findone return undefine

时间:2018-03-14 06:01:03

标签: javascript node.js mongoose

我有一个机器人。它可以输入一些文字并返回一些单词。

我想使用MongoDB。因为Heroku无法存储数据。

所以我添加了使用mongoose的function.js.

 console.log('data.functionswitch = ' + data.functionswitch);  

控制台日志工作正常。它可以回复我想要的内容。

 return data.functionswitch;

但是返回data.functionswitch只在我在input.js /.//中输入时返回undefined。

我试过async / await。

但它只会停止工作。

如何改进并使其有效?谢谢。

-

-

2018/03/15更新

function.js

function switchfind(id, name, callback) {
mongodb.functionSwitch.findOne({
    groupid: id, functionname: name
}, function (err, data) {
    if (err) {
        console.log(err);
        callback(null);
        return;
    }

    else if (!data) {
        console.log("No record found")
        callback(null);
        return;
    }
    console.log('date = ' + data);
    console.log('data.functionswitch = ' + data.functionswitch);
     callback(data.functionswitch);
    return;
})
};

input.js

function parseInput(rplyToken, inputStr) {
//console.log('InputStr: ' + inputStr);
_isNaN = function (obj) {
    return isNaN(parseInt(obj));
}

let msgSplitor = (/\S+/ig);
let mainMsg = inputStr.match(msgSplitor); 
let trigger = mainMsg[0].toString().toLowerCase(); 

exports.mongoose.switchfind(mainMsg[1], mainMsg[2], function (functionswitch) {
    console.log('functionswitch = ' + functionswitch)

    if (functionswitch === null) {
        console.log('HERE === NULL ')
    }
    if (functionswitch == 0) {
        console.log('HERE != 0')
        return;
    }
    else if (functionswitch != 0 ) {
        console.log('HERE != 0')


        if (inputStr.match(/\w/) != null && inputStr.toLowerCase().match(/\d+d+\d/) != null) return exports.rollbase.nomalDiceRoller(inputStr, mainMsg[0], mainMsg[1], mainMsg[2]);

    }

})

}

更新

const mongoose = require('mongoose');
let uristring = process.env.mongoURL ||
'mongodb://XXXXXXX';
mongoose.connect(uristring);

mongoose.connect(uristring, function (err, res) {
if (err) {
    console.log('ERROR connecting to: ' + uristring + '. ' + err);
} else {
    console.log('Succeeded connected to: ' + uristring);
    // console.log('allswitch: ' + allswitch);
}
});

var functionSchema = new mongoose.Schema({
groupid: String,
functionname: String,
functionswitch: String
});

// Compiles the schema into a model, opening (or creating, if
// nonexistent) the 'PowerUsers' collection in the MongoDB database
var functionSwitch = mongoose.model('functionSwitchs', functionSchema);

1 个答案:

答案 0 :(得分:0)

您的代码中的问题是您使用的是findOne,因为它是同步的。您不能简单地返回数据,您必须使用callback

Here是一个关于回调的教程。

它应该是什么样子的例子:

// The find function
function switchfind(id, name, callback) {
  mongodb.functionSwitch.findOne({
    groupid: id,
    functionname: name
  }, function (err, data) {
    // Handle error
    if (err) {
      callback(null);

      return;
    }

    // Handle empty data
    if (data == null) {
      callback(null);

      return;
    }

    // Handle with data
    callback(data.functionswitch);
  })
};
// How to call it
funcX() {
  switchfind(id, name, function (functionswitch) {
    if (functionswitch === null) {
      // Handle the error
    }

    // Handle the data
  });
}