Quy Quy - 10 months ago 46
Node.js Question

Mongoose inside a promise change happens late

I am writing an API in NodeJS in which I use Mongoose and BlueBird. Regarding promise chain, my data was supposed to go through waterfall functions but it didn't. Let my example start with

to get some
, feeding data to
to query and end up with

exports.getTagValues = function (req, res) {
var userId = req.params.uid;
getTagNames(req, res)
.then(function (data) {
console.log('tags', data);
res.json(200, data);
console.log('err', err);
//handle Error

Here is my toy data,

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' }

The way I query data. After querying inside mongo, I check whether or not have a document with
. In case not, insert and return document. Note
is my mongo model

function retrieveTag(data){
Tag.findOne({'userId': data.userId})
.then( function(tag){
if (tag) {
console.log('result', tag);
// do something ...
return tag;
else {
var newTag = new Tag({
advertiserId: advertiserId,
variables: variables
.then(function () {
console.log('newTag', newTag);
return newTag;

Here is my result (
), my expectation is
console.log('tags', data);
occurs after all then
should not be

tags undefined
GET /api/tag/values/1 200 3ms
newTag { __v: 0,
userId: '1',
_id: 581b96090e5916cf3f5112fe,
[ { type: 'String', name: 'hotel', id: 1 },
{ type: 'String', name: 'location', id: 2 } ] }

My question is how can I fix it. If there's some unclear, please help me correct.

Answer Source

The explanation is a bit unclear, but if I follow you right you loose data in the promise resolvement chain.

When reading your code, I notice that retrieveTag does not return the Mongoose promise. To let .then in getTagValues use the data found in retrieveTag.

So change to this:

function retrieveTag(data){
  return Tag.findOne({'userId': data.userId})
    .then( function(tag){