直接在RethinkDB中获取插入的文档

问题描述 投票:0回答:1

我一直从RethinkDB开始。我浏览了文档。我有一个简单的用例,可以在进行POST调用时返回插入的文档。这是我到目前为止所做的。

router.post('/user', jsonParser, async (req: Request, res: Response) => {
  const connection = await conn;

  const { name, email, employeeId } = req.body;
  // Email is primary key
  consumerTable.insert({
    name,
    email,
    employeeId,
  }).run(connection, (err: any, result: any) => {
    if (err) {
      throw err;
    } else {
      consumerTable.get(email).run(connection, (innerErr: any, userResult: any) => {
        if(innerErr) {
          throw innerErr;
        } else {
          res.send({
            data: {
              ...userResult
            },
            responseCode: 200,
          });
        }
      });
    }
  });
});

是否有任何方法可以在插入结果本身中获取插入的对象。我阅读了很多文档,但没有发现任何有用的信息。

任何帮助都会很棒。干杯!

database mongodb rethinkdb rethinkdb-javascript
1个回答
0
投票

您可以使用returnChanges中指定的insert命令的the documentation选项。它看起来应该像这样:

consumerTable.insert({
    name,
    email,
    employeeId,
}, { returnChanges: true }).run(connection, async (err: any, cursor: any) => {
    const data = await cursor.toArray();
    // .error should be undefined, unless... an error happened!
    // not sure about the format though, console.log data to check it really is an array
    res.send({ data: data[0].new_val, error: data[0].error });
});
© www.soinside.com 2019 - 2024. All rights reserved.