1

我开始使用 Thinky 学习和练习 RethinkDB,但是我有一个问题需要几天时间,但我无法解决。将数据存储或保存在一个集合中通常对我有用,即使我保存相同的数据。现在我正在使用两个模型进行测试,因此能够执行联接,我的模型如下:将数据存储或保存在一个集合中对我来说正常工作,即使我保存相同的数据。现在我正在使用两个模型进行测试,因此能够执行加入,我的模型如下......

  • 这是 Post 的模型:

'use strict'
let thinky = require('../util/thinky')
let type = thinky.type

let Post = thinky.createModel('Post',{
    id: type.string(),
    title: type.string(),
    content: type.string(),
    idAutor: type.string()
}) 

module.exports = Post

  • 这是 Autor 的模型:

'use strict'
const thinky = require('../util/thinky')
const type = thinky.type

let Autor = thinky.createModel("Autor",{
    id: type.string(),
    name: type.string()
})

module.exports = Autor

这是我在开始时向您提到的错误发送给我的部分。我正在做一个belongsTo,如Thinky文档中所示,如下所示:

'use strict'
const Post = require('../models/postModel')
const Autor = require('../models/autorModel')

async function saludar(req,res){

    Post.hasOne(Autor, "autor", "idAutor", "id") //Here is the line where the error sends me

    try {
        let post = new Post({
            title: "title",
            content: "content"
        })
        let autor = new Autor({
            name: "name"
        })
        post.autor = autor
        await post.saveAll().then(function(result){
            res.status(200).json({
                ok: true,
                result: result
            })
        }).catch((error)=>{
            res.status(500).json({
                ok: false,
                message: `Error: ${error}`
            })   
        })
    } catch (error) {
        res.status(500).json({
            ok: false,
            message: `Error: ${error}`
        })
    }
}

我第一次执行此操作时,它通常会保存数据并显示如下:

{
  "ok": true,
  "result": {
    "title": "title",
    "content": "content",
    "autor": {
      "name": "name",
      "id": "b53ab88e-7130-497f-ab4b-d6973bd640af"
    },
    "id": "6abef79c-5004-48bc-9e78-6e8f9c8d8b33"
  }
}

但是当第二次存储其他数据甚至相同数据时,它已经向我发送了错误:

POST /api/registro 200 137.700 ms - 170
(node:14378) UnhandledPromiseRejectionWarning: Error: The field `autor` is already used by another relation.
    at Function.Model.hasOne (/home/nik/Documents/rest-api-atlanta/node_modules/thinky/lib/model.js:392:11)
    at saludar (/home/nik/Documents/rest-api-atlanta/controllers/index.js:10:10)
    at Layer.handle [as handle_request] (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/layer.js:95:5)
    at /home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:335:12)
    at next (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:174:3)
    at router (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:317:13)
    at /home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:335:12)
    at next (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:275:10)
    at logger (/home/nik/Documents/rest-api-atlanta/node_modules/morgan/index.js:144:5)
    at Layer.handle [as handle_request] (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:317:13)
    at /home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:335:12)
    at next (/home/nik/Documents/rest-api-atlanta/node_modules/express/lib/router/index.js:275:10)
(node:14378) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14378) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

如果有人可以解释原因,我已经好几天没有理解了,或者我可能需要做一些额外的配置。提前致谢...

4

0 回答 0