Prisma javascript client usage


#1

Hello everybody,

I’m currently trying to dive in into prisma.

Unfortunately I’m not that proficient in the use of async/await. I’ve got the following code:

var express = require('express');
var router = express.Router();
const {
    Prisma
} = require("../db_client")

router.post('/:varname', function (req, res) {
    //Test: Varname Posten
    varname = req.params.name;
    nova = writeVarname(varname)
    res.send(nova)
 });


async function writeVarname(name) {
        const prisma = new Prisma({});
        const newVar = await prisma.createVariant({
            name: varname
        });
        return newVar;        
}

As far as I can see it should work with the autogenerated prisma client. However I get this error message:

(node:8208) UnhandledPromiseRejectionWarning: Error: Variable '$data' expected value of type 'VariantCreateInput!' but got: {}. Reason:
 'name' Expected non-null value, found null. (line 1, column 11):
mutation ($data: VariantCreateInput!) {
          ^
    at BatchedGraphQLClient.<anonymous> (D:\code\dnw\aurora\maprender\node_modules\http-link-dataloader\dist\src\BatchedGraphQLClient.j
s:77:35)
    at step (D:\code\dnw\aurora\maprender\node_modules\http-link-dataloader\dist\src\BatchedGraphQLClient.js:40:23)
    at Object.next (D:\code\dnw\aurora\maprender\node_modules\http-link-dataloader\dist\src\BatchedGraphQLClient.js:21:53)
    at fulfilled (D:\code\dnw\aurora\maprender\node_modules\http-link-dataloader\dist\src\BatchedGraphQLClient.js:12:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:8208) 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(). (rejection id: 1)
(node:8208) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not h
andled will terminate the Node.js process with a non-zero exit code.

Does anyone have an idea how to fix this?
Thanks in advance for your help, I really appreciate it.


#2

Hey @Wulfheart,

can you please post your Prisma datamodel?

The error message seems to suggest that the createVariant function didn’t receive a name in its input argument object.

 Error: Variable '$data' expected value of type 'VariantCreateInput!' but got: {}. Reason:
 'name' Expected non-null value, found null. (line 1, column 11):
mutation ($data: VariantCreateInput!)

Looking at your code, it seems that the varname variable inside writeVarname doesn’t have a value (i.e. its null). That’s why the Prisma API complains, I assume the name on your Variant type is required?

You can fix this by ensuring that varname has a value.

Another quick hint, there’s no need to instantiate the Prisma client every time you invoke writeVarname. Instead you can directly import the Prisma client instance:

const {
    prisma
} = require("../db_client")

That way, you can remove this line from writeVarname:

const prisma = new Prisma({});

#3

Hello @nikolas,
thanks for your quick response.
I think I got my error. I tried to get the varname-Parameter but fetched a parameter called name.
Thanks for your helpful hint.