Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: obj.hasOwnProperty is not a function when calling Graphql mutation

I get a strange error and can't figure out what I am doing wrong. I wrote a graphql mutation to call an api:

domainStuff: async (parent, { command, params }, { models }) => {
  console.log("Params:", params);
  const result = await dd24Api(command, params);
  return result;
}

This it the function I call:

export default async (command, parameter) => {
  const args = _.merge({ params: parameter }, auth);
  // Eleminate copying mistakes
  console.log(typeof args);
  const properCommand = command + "Async";
  const result = await soap
    .createClientAsync(apiWSDL)
    .then(client => {
       return client[properCommand](args)
         .then(res => {
            console.log(res[command + "Result"]);
          return res[command + "Result"];
    })
    .catch(err => {
      console.log(err);
      return err;
      });
     })
   .catch(err => console.log(err));

return result;

with this query variables:

{
"command": "CheckDomain",
"params": {"domain": "test.it"}
}

The console.log shows me that args is an object, but I get this error (from the first catch block):

TypeError: obj.hasOwnProperty is not a function

How can that be? After all I checked whether it is an object and it is. More strange, if I give a hardcoded object into the query, this for example:

domainStuff: async (parent, { command, params }, { models }) => {
  console.log("Params:", params);
  const result = await dd24Api(command, {domain: "test.com"});
  return result;
}

then it works perfectly fine. What am I doing wrong? Thx for any help in advance.

EDIT: I am using "graphql-server-express": "^0.8.0" and "graphql-tools": "^1.0.0"

like image 691
Gh05d Avatar asked Dec 18 '22 02:12

Gh05d


2 Answers

If you are using graphql-js there are a lot of places where new objects are created using Object.create(null) which is different from {} you can read an explanation about that here Creating Js object with Object.create(null)?

But essentially an object created with Object.create(null) has no hasOwnProperty method

You can try it in node with

const obj1 = Object.create(null)
console.log(typeof obj1.hasOwnProperty)
// 'undefined'
const obj2 = {}
console.log(typeof obj2.hasOwnProperty)
// 'function'

Another method you can use for determining if an object has a key that will work on an object created with Object.create(null) is

function hasKey(obj, key) {
  return Object.keys(obj).indexOf(key) !== -1
}
like image 67
vbranden Avatar answered Dec 20 '22 17:12

vbranden


You can convert it to a standard Object by:

data = JSON.parse(JSON.stringify(data));
like image 32
ttfreeman Avatar answered Dec 20 '22 18:12

ttfreeman