1
私はMongooseとBlueBirdを使うNodeJSにAPIを書いています。プロミスチェーンに関しては、私のデータは滝の機能を通過するはずだったが、それはなかった。私の例をgetTagNames
で始めると、が得られ、retrieveTag
にデータを入力して照会し、res.json()
になります。約束が変わる中のマングースは遅くなります
exports.getTagValues = function (req, res) {
var userId = req.params.uid;
getTagNames(req, res)
.then(retrieveTag)
.then(function (data) {
console.log('tags', data);
res.json(200, data);
})
.catch(function(err){
console.log('err', err);
//handle Error
})
}
は、ここに私のおもちゃのデータ、
function getTagNames(req, res) {
var userId = req.params.uid;
return new Promise.resolve({
'userId': userId,
'variables': [
{ id: 1, name: 'hotel', type: 'String' },
{ id: 2, name: 'location', type: 'String' }
],
})
}
私はデータを照会方法です。 mongoの中身を調べた後、userID
という文書があるかどうかを調べます。そうでない場合は、文書を挿入して返却してください。注Tag
はここ
function retrieveTag(data){
Tag.findOne({'userId': data.userId})
.exec()
.then(function(tag){
if (tag) {
console.log('result', tag);
// do something ...
return tag;
}
else {
var newTag = new Tag({
advertiserId: advertiserId,
variables: variables
});
newTag.save()
.then(function() {
console.log('newTag', newTag);
return newTag;
});
}
})
}
は私の結果(
userId
が
1
です)、私の期待は
console.log('tags', data);
が、その後
data
は、私の質問は、私が修正できる方法です
undefined
tags undefined
GET /api/tag/values/1 200 3ms
newTag { __v: 0,
userId: '1',
_id: 581b96090e5916cf3f5112fe,
variables:
[ { type: 'String', name: 'hotel', id: 1 },
{ type: 'String', name: 'location', id: 2 } ] }
をすべきではない結局のところ発生である私のmongoモデルであり、それ。不明な点がある場合は、修正してください。