db.all函数不作为数组变量返回

时间:2019-10-30 20:45:40

标签: javascript sql node.js json express

我想从db.all函数接收一个数组,以便在我想返回它作为响应的自定义JSON对象中使用它

我尝试过 Object.values(成员); Members.fulfillmentValue; 我在StackOverFlow中找到的一些PROMISE方法

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);
});

实际反应

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

预期的反应

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

1 个答案:

答案 0 :(得分:0)

我通过添加异步解决了它

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);
});

感谢所有尝试提供帮助的人