Error saving data to RethinkDB using Thinky

42 views Asked by At

I'm starting to study and practice RethinkDB using Thinky, but I have a problem that takes a couple of days and I can't solve it. Storing or saving data in just one collection works for me normally, even if I save the same data. Now I am testing with two models and thus be able to perform a Join, my models are the following:Storing or saving data in just one collection works for me normally, even if I save the same data. Now I am testing with two models and thus be able to perform a Join, my models are the following...

  • This is Post's model:

'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

  • This is Autor's model:

'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

Here is the part where the error that I mentioned to you at the beginning sends me. I'm doing a belongsTo as shown in the Thinky documentation as follows:

'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}`
        })
    }
}

The first time I do this operation, it normally saves the data and shows me as follows:

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

But when storing other data and even the same data for the second time onwards, it already sends me the error:

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.

If someone could explain why, I haven't understood for days, or maybe I have to do some additional configuration. Thanks in advance...

0

There are 0 answers