使用Thinky将数据保存到RethinkDB时出错

问题描述

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

  • 这是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",name: type.string()
})

module.exports = Autor

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

'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.

如果有人可以解释原因,那么我已经好几天了,或者也许我必须做一些其他配置。预先感谢...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)