如何获取我最后创建的实例的 ref(id)

How to get the ref(id) of my last instance created

我不知道如何简单地获取我用 faunadb 创建的最后一个实例的 ref(id)。 我需要把它放在 url.

我用它来创建我的实例:

/* code from functions/todos-create.js */
import faunadb from 'faunadb' /* Import faunaDB sdk */
/* configure faunaDB Client with our secret */
const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET
})

/* export our lambda function as named "handler" export */
exports.handler = (event, context, callback) => {
  /* parse the string body into a useable JS object */


  const eventBody = JSON.stringify(event.body)
  const data = JSON.parse(eventBody)
  const mission = {
    data: JSON.parse(data)
  }
  // {"title":"What I had for breakfast ..","completed":true}
  /* construct the fauna query */
  return client.query(q.Create(q.Ref("classes/missions"), mission))
  .then((response) => {
    console.log("success", response)
    /* Success! return the response with statusCode 200 */
    return callback(null, {
      statusCode: 200,
      body: JSON.stringify(response)
    })
  }).catch((error) => {
    console.log("error", error)
    /* Error! return the error with statusCode 400 */
    return callback(null, {
      statusCode: 400,
      body: JSON.stringify(error)
    })
  })
}

我开始开发,我需要用 faunadb 做这个,你能帮我吗?

创建实例 returns a ref, as along with other instance metadata and the user supplied instance data. This means we can compose a select 并使用 create 提取您需要的数据。使用 shell 语法:

Select("ref", Create(Class("missions"), {...})

将产生参考。当然,如果您只想要 ref 的 id 部分,您可以进一步研究:

Select(["ref", "id"], Create(Class("missions"), {...})