简体   繁体   中英

db.all function doesn't return as an array variable

I want to receive an array from db.all function in order to use it in my custom JSON object that I want to return it as a response

I have tried Object.values(members); members.fulfillmentValue; some PROMISE methods I found in StackOverFlow

app.get('/group/:groupId', (req, res, next) => {

    const members= db.all('SELECT name,active FROM members WHERE groupId = ?;', req.params.groupId);

    return db.get('SELECT * FROM groups WHERE groups.id = ?;',req.params.groupId)
     .then(group =>  res.json({
         group.name,
         members:[members]
        }))
        .catch(next);
});

actual response

{
   "name":"group 1",
   "memebers":[
      {"isFulfilled":true,"isRejected":false,"fulfillmentValue":[
         {"name":"Member 1","active":1},
         {"name":"Member 2","active":0}
      ]
   }]
}

expected response

{
   "name":"group 1",
   "memebers":[
      {"name":"Member 1","active":1},
      {"name":"Member 2","active":0}
   ]
}

I have solved it by adding async

app.get('/group/:groupId', async (req, res, next) => {

    const members= await db.all('SELECT name,active FROM members WHERE groupId = ?;', req.params.groupId).then();

    return db.get('SELECT * FROM groups WHERE groups.id = ?;',req.params.groupId)
     .then(group =>  res.json({
         group.name,
         members:members
        }))
        .catch(next);
});

thanks to all who tried to help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM