爲什麼不會usernametoid
函數返回什麼?我知道它通過安慰它存在,但它不會存儲在otherplayerid
變量?爲什麼?Node.js不會返回任何東西?
我的應用程序:(調用API後殺)
var userFunc = require('../factory/user_factory.js');
app.post('/api/kill', function (req, res) {
var username = "signature";//req.query.username;
var otherplayerid = userFunc.usernametoid(username);
if (!(otherplayerid)) {
console.log("other player is acually " + otherplayerid);
result.push("denne brukeren finnes ikke! " + otherplayerid);
} else {
}
});
和我user_factory:
var articles = require('../controllers/articles.server.controller'),
path = require('path'),
mongoose = require('mongoose'),
Article = mongoose.model('Article'),
Users = mongoose.model('User'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
exports.usernametoid = usernametoid;
function usernametoid(id) {
var query = Users.findOne({ username : id });
query.exec(function(err, datas) {
console.log(datas._id);
return datas._id;
});
}
控制檯:
other player is acually undefined
57c1c0f3b6b20c011242bf22
你的控制檯輸出是什麼樣的? – Zeokav
您試圖從異步操作返回信息。你不能那樣做。 – jfriend00
我會怎麼做然後@ jfriend00? – maria