Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 6, 2018 11:45
Show Gist options
  • Save luojiyin1987/34572797f7be4cadd4310f54335ee75a to your computer and use it in GitHub Desktop.
Save luojiyin1987/34572797f7be4cadd4310f54335ee75a to your computer and use it in GitHub Desktop.
要抽象的异步调用 #callback #moogoose
// 代码
const keystone = require('keystone');
const SMSLog = keystone.list('SMS_log');
const user = keystone.list('User');
const Wallet = keystone.list('Wallet');
exports = module.exports = function (req, res) {
console.log(req.query.id);
const id = req.query.id;
Wallet.model.find()
.where('mobile', id)
.exec(function(err ,Wallets){
if (err) console.log(err);
if ( Wallets.length === 0) {
res.json({ message: 'no walletaddr' });
} else {
res.json({ message: 'has walletaddr',
walletAddr: Wallets[0].address });
}
})
}
//-----------------------------------------------------------------
// 我想把下面的代码抽象出一个可以调用的函数
Wallet.model.find()
.where('mobile', id)
.exec(function(err ,Wallets){
if (err) console.log(err);
if ( Wallets.length === 0) {
res.json({ message: 'no walletaddr' });
} else {
res.json({ message: 'has walletaddr',
walletAddr: Wallets[0].address });
}
})
//-----------------------------------------------------------------
否则以后逻辑复杂后, 将死得很难看
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment