NodeJS, Mongoose, return variable
NodeJS, Mongoose, return variable
提前感谢您的时间和回答:
我一直在尝试做一些"应该"容易的事情,但这让我很疯狂。
目标是将函数的结果分配给一个变量,我稍后可以在 POST 上使用该变量将信息发送到我的 MongoDB。
我有一个带有文档的模型:
{ "__v" : 0, "_id" : ObjectId("54ad1aa637ce5c566c13d18f"), "num" : 9 }
我想要的是捕获这个数字"num":9到一个变量。我创建了一个通过模型查询 mongodb 的函数。
function getNum(){
var Num = require('./models/num.js');
var callback = function(){
return function(err, data) {
if(err) {
console.log("error found: " + err);
}
console.log("the number is: " + data.num);
}
};
return Num.findOne({}, callback());
};
然后只是为了测试,我将该函数分配给一个变量并尝试控制台.log它只是为了测试结果是否正常。
// =====================================
// TESTING ==============================
// =====================================
app.get('/testing',function(req, res, next){
var a = getNum();
console.log(a);
});
我的输出是:
{ _mongooseOptions: {},
mongooseCollection:
{ collection:
{ db: [Object],
collectionName: 'num',
internalHint: null,
opts: {},
slaveOk: false,
serializeFunctions: false,
raw: false,
pkFactory: [Object],
serverCapabilities: undefined },
opts: { bufferCommands: true, capped: false },
name: 'num',
conn:
{ base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'project',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: {},
db: [Object] },
queue: [],
buffer: false },
model:
{ [Function: model]
base:
{ connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
modelName: 'Num',
model: [Function: model],
db:
{ base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'project',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: {},
db: [Object] },
discriminators: undefined,
schema:
{ paths: [Object],
subpaths: {},
virtuals: [Object],
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
options: [Object],
_events: {} },
options: undefined,
collection:
{ collection: [Object],
opts: [Object],
name: 'num',
conn: [Object],
queue: [],
buffer: false } },
op: 'findOne',
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
{ collection:
{ collection: [Object],
opts: [Object],
name: 'num',
conn: [Object],
queue: [],
buffer: false } },
_castError: null }
**the number is: 9**
这是我在其他未定义的问题中可以得到的结果之一。
谁想知道我能做些什么来解决这个问题?
再次感谢您的帮助。
不能像 findOne
那样返回异步函数的结果,需要使用回调。
因此,需要将其重写为:
function getNum(callback){
var Num = require('./models/num.js');
return Num.findOne({}, callback);
};
app.get('/testing',function(req, res, next){
getNum(function(err, a) {
console.log(a);
});
});
相关文章:
- 从jQuery调用存储在Variable中的函数
- jquery if语句返回return wong语句
- 函数在return语句之前返回空对象
- Javascript Return and if/else
- 在ES6 Promise中,我应该在解决/拒绝之前使用return吗
- 将Transform和Instance Variable转换为对象的JavaScript数组
- Angular promise return"未定义的“;值.NET MVC
- Javascript用函数return替换局部变量
- Php variable obj to js
- JavaScript中的“return cb()”有缺点吗
- JS-fn()中返回后方括号的含义{return{}[]}
- 将Computed PHP Variable传递到jQuery/Ajax表单
- jQuery .load with variable as url
- 为什么javascript在'return false'
- 获取Div Width,然后更新Scope Variable
- return false不阻止点击
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- 当使用return语句时,循环不会中断
- ColdFusion XML to Javascript variable
- NodeJS, Mongoose, return variable