A
A
Anrek2021-12-09 14:42:21
PostgreSQL
Anrek, 2021-12-09 14:42:21

How to fix an error when accessing the database?

Hello!

There is a project with a git on a node with slonik (a library for interacting with the database), I launched it, I see how it works. I send requests to him from Postman - if it's just on api - the request works, there are no problems. If interaction with the database is required, then an error occurs:

TypeError: slonik_1.sql.raw is not a function.

earlier I downgraded slonik because in the same places (with sql.raw) another error came out: Property 'raw' does not exist on type. There, the library was placed in a strange way and no reference was made to the raw interface.

i googled the error "TypeError: slonik_1.sql.raw is not a function." - there is a similar solution here, but this is either not my case or I do not have enough knowledge and experience to bring it to my case. There db.query is suggested to replace db.serialize.query. I tried instead of:

slonik_1.sql.raw

to do:
slonik_1.sql.serialize.raw

but it didn't help - my code doesn't know serialize type for sql.

Question: what can I try to do to fix the error?

Additional information to the question:
1) About what I do:
- The project is a backend for a mobile application: it processes requests/receives/sends data. I want to understand how the back-end works. To do this, I deployed it on the local host and connected it to the Postgresql database also on the local server (although I have no exact confidence that the connection to the database has been established, but there are no errors when determining the database url). Because I don't have a mobile application, I use Postman as a client.
- The backend has an Api file where requests are processed. First, I checked the GET request by simply inserting the request processing right away with the data into the Api file (so as not to touch the database) and sent the request to the port with the application with Postman. Everything is OK, the request has returned, the data has been brought.
- Now I want to go deeper and get to the database: make a Post request in Postman so that the backend writes it to the database. But I get an "internal" error in response to a data write request in Postman, and an error in visual studio code: TypeError: slonik_1.sql.raw is not a function.

2) Error in more detail:

In more detail, the error looks like this (the application does not crash):

TypeError: slonik_1.sql.raw is not a function
    at Object.getToken (C:\Users\user\Desktop\backend\dist\db.js:87:32)
    at C:\Users\user\Desktop\backend\dist\api.js:336:32
    at C:\Users\user\Desktop\backend\dist\api.js:278:30
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\backend\node_modules\express\lib\router\layer.js:95:5)
    at next (C:Users\user\Desktop\backend\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\user\Desktop\backend\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\backend\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:281:22   
    at Function.process_params (C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\user\Desktop\backend\dist\api.js:68:5
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\backend\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:284:7    
    at Function.process_params (C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\user\Desktop\backend\node_modules\express\lib\router\index.js:275:10)


3) stack of tools (from packege.json):
google_maps, sindresorhus/slugify, bcrypt, bcryptjs, body-parser, cors, cron, express, fcm-push, lodash, ms, node-fetch, pg, reflect-metadata, sendgrid , slonik, socket.io

itself backend on node js on ts.

4) general view of the code:
in addition to api - where is the processing of requests (according to the principle app.post('/someReq', function cheking mistakesAndToken, await db.doSomething) separately there is a db file that is imported into api.app

is
export const app = Express();
bd is a
file with functions for accessing the database using slonik

5) places where the error is more detailed:

errors of the form:
Property 'raw' does not exist on type 'SqlTaggedTemplateType'.;

almost everywhere in the junctions of sql with something (sql from here: import { createPool, sql } from 'slonik'). If you downgrade the version of the elephant - the errors will go away - but this will not affect the results of the POST request from Postman in any way (in both Postman and Visual Studio).

Example lines where errors:
const escapeNames = (names: string[]) => sql.raw(names.map(x => escapeName(x).sql).join(', ')); //(raw - подчеркивается)

const svalue = value => sql.valueList([value]); //(valueList- подчеркивается)

function _insert(obj) {
  const schema = Schema.get(obj);
  const keys = schema.cols.map(x => x[0]).filter(x => !schema.serials.includes(x));
  return sql`
    INSERT INTO ${escapeName(schema.name)} (${escapeNames(keys)})
      VALUES ${sql.tuple(keys.map(x => obj[x]))}
  `;//(.tuple - подчеркивается)
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anrek, 2021-12-10
@Anrek

I found a solution: I read about slonik and its versions, found out that they replaced the expressions in which I had an error as indicated here:
sql.raw

G
galaxy, 2021-12-09
@galaxy

This question is not about Postgresql, there is nothing to even cling to. Slonik is a command shell for Slony-I from what I know. Here, apparently, another elephant of some kind.
And here Postman, api, who is db.query/db.serialize.query - is unknown.
Describe the stack of tools you use, the sequence of actions, show the code.
And put the tags (if it's a fucking sequelize put Node.js)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question