Javascript 存储MongoDB结果(承诺待定)
我一直试图将MongoDB字段值存储为变量,但我得到了Javascript 存储MongoDB结果(承诺待定),javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我一直试图将MongoDB字段值存储为变量,但我得到了承诺{}。我一直以为我知道如何发出异步请求,但似乎我真的不知道 exports.main = (req, res, next) => { const dt = Post.findOne({ active: true }, function(err, data) { if (err) throw err; return data; }).sort({ $natural: -1 }).
承诺{}
。我一直以为我知道如何发出异步请求,但似乎我真的不知道
exports.main = (req, res, next) => {
const dt = Post.findOne({ active: true }, function(err, data) {
if (err) throw err;
return data;
}).sort({ $natural: -1 }).exec().then(doc => {
return(doc);
});
// Logs --> Promise { <pending> }
console.log(dt);
}
exports.main=(请求、恢复、下一步)=>{
const dt=Post.findOne({active:true},函数(err,data){
如果(错误)抛出错误;
返回数据;
}).sort({$natural:-1}).exec().then(doc=>{
退货(doc);
});
//日志-->承诺{}
控制台日志(dt);
}
如果您想访问查询结果,您应该在回调
或中工作。然后
查询:
exports.main = (req, res, next) => {
let promise = Post.findOne({ active: true }).sort({ $natural: -1 }).exec();
promise.then((doc) => {
console.log(doc);
res.send(doc); // send data if needed. I think this is express route handler
})
.catch((err) => {
console.log(err);
next();
})
}
如果要访问查询结果,应在
回调
或中工作。然后
查询:
exports.main = (req, res, next) => {
let promise = Post.findOne({ active: true }).sort({ $natural: -1 }).exec();
promise.then((doc) => {
console.log(doc);
res.send(doc); // send data if needed. I think this is express route handler
})
.catch((err) => {
console.log(err);
next();
})
}
这是一个类似于的问题 所有依赖于查询结果的操作都应在
内部执行,然后执行:
Post.findOne({ active: true }).sort({ $natural: -1 }).exec().then(dt => {
...
});
findOne
承诺不需要回调参数
async..await
通常可用于类似同步的控制流:
exports.main = async (req, res, next) => {
try {
const dt = await Post.findOne({ active: true }).sort({ $natural: -1 }).exec();
...
next(); // if necessary
} catch (error) {
next(error);
}
}
如中所述,由于Express不处理承诺,因此开发人员应通过将整个async
函数体包装为try..catch
来处理所有错误。这是一个类似于的问题
所有依赖于查询结果的操作都应在内部执行,然后执行:
Post.findOne({ active: true }).sort({ $natural: -1 }).exec().then(dt => {
...
});
findOne
承诺不需要回调参数
async..await
通常可用于类似同步的控制流:
exports.main = async (req, res, next) => {
try {
const dt = await Post.findOne({ active: true }).sort({ $natural: -1 }).exec();
...
next(); // if necessary
} catch (error) {
next(error);
}
}
如中所述,由于Express不处理承诺,所有错误应由开发人员通过使用try..catch
将整个async
函数体包装成try..catch来处理。Mongoose方法为您提供了一个完整的承诺,当您在
...exec().then(doc => {
return(doc); // <-- returns a pending promise
});
使用回调函数
exports.main = (req, res, next) => {
Post.findOne({ active: true }).sort({ $natural: -1 }).exec((err, data) => {
if (err) throw err;
console.log(data);
res.status(200).send(data);
});
}
使用承诺
exports.main = (req, res, next) => {
Post.findOne({ active: true }).sort({ $natural: -1 }).exec().then(data => {
console.log(data);
res.status(200).send(data);
}).catch(err => {
console.error(err);
next();
});
}
Mongoose方法为您提供了一个完整的承诺,当您在
...exec().then(doc => {
return(doc); // <-- returns a pending promise
});
使用回调函数
exports.main = (req, res, next) => {
Post.findOne({ active: true }).sort({ $natural: -1 }).exec((err, data) => {
if (err) throw err;
console.log(data);
res.status(200).send(data);
});
}
使用承诺
exports.main = (req, res, next) => {
Post.findOne({ active: true }).sort({ $natural: -1 }).exec().then(data => {
console.log(data);
res.status(200).send(data);
}).catch(err => {
console.error(err);
next();
});
}