GQL queries not working using gcloud (google-cloud) and node.js - node.js

I am trying to run the following GQL query:
function gqlExample(callback) {
datastore.runQuery({
gqlQuery: {
queryString: 'SELECT * FROM Person',
}
}).execute(function(err, result) {
if (!err) {
// Iterate over the results and return the entities.
result = (result.batch.entityResults || []).map(
function(entityResult) {
return entityResult.entity;
});
}
callback(err, result);
});
}
I found the above example on the following page:
http://ec2-54-66-129-240.ap-southeast-2.compute.amazonaws.com/httrack/docs/cloud.google.com/datastore/docs/concepts/gql.html
It states that it is a valid node.js example but on running the query I get the following error:
/Users/xxxx/relay-fullstack/server/data/campaign-datastore.js:245
}).execute(function (err, result) {
^
TypeError: ds.runQuery(...).execute is not a function
at Object.campaignSearchGql (campaign-datastore.js:265:6)
at Test.<anonymous> (datastore-test.js:29:16)
at Test.bound [as _cb] (/Users/xxxx/relay-fullstack/node_modules/tape/lib/test.js:63:32)
at Test.run (/Users/xxxx/relay-fullstack/node_modules/tape/lib/test.js:82:10)
at Test.bound [as run] (/Users/xxxx/relay-fullstack/node_modules/tape/lib/test.js:63:32)
at Immediate.next [as _onImmediate] (/Users/xxxx/relay-fullstack/node_modules/tape/lib/results.js:70:15)
at tryOnImmediate (timers.js:534:15)
at processImmediate [as _immediateCallback] (timers.js:514:5)
I am using the latest version of google cloud for node:
"google-cloud": "^0.38.3",
Has anyone got GQL queries executing correctly with gcloud and node? Any help will be greatly appreciated.

That is apparently a cached version of the Datastore docs. Those Node.js snippets are using a different library called googleapis: https://github.com/google/google-api-nodejs-client
The other library, gcloud-node, doesn't support GQL at this time.

Related

Cannot find module 'posts.graphql' using loadSchema and GraphQLFileLoader from graphql-tools

I am trying to load schema from a specific folder.
I am trying to not using Apollo, a pretty good solution that I found is loadSchema from #graphql-tools/load.
const { loadSchema } = require('#graphql-tools/load');
const { GraphQLFileLoader } = require('#graphql-tools/graphql-file-loader');
async function getLoadedSchema() {
const schemas = await loadSchema('/absolute/path/*.graphql', {
loaders: [new GraphQLFileLoader()]
})
console.log('mySchema', schemas);
}
By launching the server, I get this error:
Running a GraphQL API server at http://localhost:4000/graphql
(node:30146) UnhandledPromiseRejectionWarning: Error: Cannot find module 'posts.graphql'
Require stack:
- /absolute/path/src/routes/users/create/noop.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/myUser/.asdf/installs/nodejs/12.22.1/.npm/lib/node_modules/ts-node/node_modules/#cspotcode/source-map-support/source-map-support.js:679:30)
at resolveFileName (/absolute/path/node_modules/resolve-from/index.js:29:39)
at resolveFrom (/absolute/path/node_modules/resolve-from/index.js:43:9)
at module.exports (/absolute/path/node_modules/resolve-from/index.js:46:47)
at resolveFilePath (/absolute/path/node_modules/#graphql-tools/import/index.js:395:20)
at visitFile (/absolute/path/node_modules/#graphql-tools/import/index.js:57:20)
at processImports (/absolute/path/node_modules/#graphql-tools/import/index.js:291:41)
at visitFile (/absolute/path/node_modules/#graphql-tools/import/index.js:66:82)
at Object.processImport (/absolute/path/node_modules/#graphql-tools/import/index.js:36:1
I don't really know where the comes from.
Do you have some ideas ?

Error after upgrading Joi to latest version - Schema can only contain plain objects (name)

After upgrading Joi to the latest version #hapi/Joi(17.1.1) my server is not staring I am getting below error on startup. Seems there were some breaking changes in recent versions. Not able to get any clue yet, any help is appreciated.
Error: Schema can only contain plain objects (name)
at new module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/error.js:23:19)
at module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/assert.js:20:11)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:88:5)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/base.js:631:24)
at /Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:92
at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/common.js:173:16)
at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:32)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:90:25)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:117:24)
at Object.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/index.js:123:24)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/validation.js:49:22)
at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:197:43)
at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:122:14)
at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:498:23)
at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:491:22)
at /Users/xyz/project/projectxyz/src/app.js:73:14
at Array.forEach (<anonymous>)
at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
path: 'name'
} Server Init error Error: Schema can only contain plain objects (name)
at new module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/error.js:23:19)
at module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/assert.js:20:11)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:88:5)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/base.js:631:24)
at /Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:92
at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/common.js:173:16)
at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:32)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:90:25)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:117:24)
at Object.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/index.js:123:24)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/validation.js:49:22)
at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:197:43)
at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:122:14)
at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:498:23)
at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:491:22)
at /Users/xyz/project/projectxyz/src/app.js:73:14
at Array.forEach (<anonymous>)
at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
path: 'name'
}
To anyone who finds themselves in this dark place again, for me it was an issue when extending a Joi schema using the spread operator:
joi.object().keys({
...otherSchema,
position: joi.number()
})
This was causing the "Schema can only contain plain objects" error when I upgraded to Joi 17.5.0. Using the keys() syntax got around it:
otherSchema.keys({
position: joi.number()
})
Thanks all for your comments. I was able to resolve it after going through Joi's release notes.
The problem was due to the mix of Joi versions. In my codebase older version of Joi was used which was causing this. Below Github issue helped me realizing the issue.
https://github.com/hapijs/joi/issues/1913
i think u should do some thing like this to handle this error:
var ObjectJoi = Joi.object({
//some item that you want to add
})
var mySchema = new Schema(Joigoose.convert(ObjectJoi))
const myModel = mongoose.model('myModel', mySchema);
It's hard to tell without seeing the code.
But I faced the same issue when using Joi.extend().
const extendedType = Joi.extend(Joi => {
return {
type: 'myType', // <- PAY ATTENTION
base: Joi.array(),
messages: {
'stringArray:base': '...'
},
validate(value, helpers) {
...
},
coerce(value, helpers) {
...
}
}
});
The problem was that I used extendedType like below by mistake:
Joi.object({
someKey: extendedType,
...
});
instead of:
Joi.object({
someKey: extendedType.myType(),
...
});

How to remove TypeError: "x" is not a function?

I am trying to learn how to make a discord bot and pull data from this API called Ergast (http://ergast.com/mrd). I found this npm (https://github.com/estevE11/f1-stats) which uses NodeJS implementation to get a historical record of F1 data from Ergast API. Sorry for the bad wording I am still trying to learn the lingo.
I followed what was stated in the npm documentation for installing it and tried using the example to get data from the API. However when I run the code in index.js I get the error "TypeError: "x" is not a function". When I go into the node_modules "f1-stats" folder and run the code from main.js I do get the correct result.
index.js:
const client = new Discord.Client(); //This will be our client
const { prefix, token } = require('./config.json');//const PREFIX = '!';
const f1s = require('f1-stats');
//module.exports.f1s = f1s; //Still causes the TypeError
f1s("2008 drivers", (res) => {
console.log(res);
});
The error message I get in index.js:
f1s("2008 drivers", (res) => {
^
TypeError: f1s is not a function
at Object.<anonymous> (C:\Users\RyanPC\Documents\DiscordBot\index.js:8:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
node_modules/f1-stats/main.js:
const f1s = require("./f1-stats"); // "./" is used because module is located in the same folder as the Node.js file
f1s("2008 drivers", (res) => {
console.log(res);
});
when I run it in node_modules/f1-stats/main.js:
{ MRData:
{ xmlns: 'http://ergast.com/mrd/1.4',
series: 'f1',
url: 'http://ergast.com/api/f1/2008/drivers.json',
limit: '30',
offset: '0',
total: '22',
DriverTable: { season: '2008', Drivers: [Array] } } }
Because f1-stats doesn't export anything so when you import it. It is empty. The correct file you need to import is f1-stats/f1-stats.
const f1s = require('f1-stats/f1-stats');
try importing f1s properly , you are getting error because you have not imported the function properly , in other words...check what is being exported from what are you trying to import....hope it solves the problem.

Type Error: Cannot read property 'hasListCollectionsCommand' of null

I'm trying to get all collection names in mongodb (v4.0.2) and using mongodb#3.1.10 nodejs driver. From the docs, I can see that listCollections might work, but running the following:
const Db = require('mongodb').Db
const Server = require('mongodb').Server
const db = new Db(dbName, new Server(host, port, options), {})
db.listCollections().toArray((error, collectionNames) => {
console.error(error)
console.log(JSON.stringify(collectionNames))
})
… leads to this error:
if (this.serverConfig.capabilities().hasListCollectionsCommand) {
TypeError: Cannot read property 'hasListCollectionsCommand' of null
at Db.listCollections ({path}/node_modules/mongodb/lib/db.js:493:39)
at db.createCollection ({path}/sampleMongoDB.js:19:8)
at err ({path}/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback ({path}/node_modules/mongodb/lib/utils.js:404:25)
at executeOperation ({path}/node_modules/mongodb/lib/utils.js:422:7)
at Db. ({path}/node_modules/mongodb/lib/db.js:431:12)
at Db.deprecated [as createCollection] ({path}/node_modules/mongodb /lib/utils.js:661:17)
at Object.< anonymous > ({path}/sampleMongoDB.js:18:6)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
My questions are:
Is there a docs site for MongoDB NodeJs drivers where we can refer examples from?
If not, what's the correct way to query?
I was able to work it out. The MongoClient's instance returns a db instance, which inturn helps run mongodb.Db methods.
Here's a sample code that worked (Ref. to MongoDB guides)
const options = {
useNewUrlParser: true, // Add this, if you wish to avoid {https://stackoverflow.com/questions/50448272/avoid-current-url-string-parser-is-deprecated-warning-by-setting-usenewurlpars} issue
}
const client = new MongoClient(connectionUri, options)
return client.connect().then(() => {
const db = client.db(dbName)
return db.listCollections()
.toArray()
.then((collections) => {
// Collections array
})
})
#Community mods - if required, kindly close this question.

Node.js Callback Failing - Function pass Variables with Callback

I'm new to Node.js and I'm struggling to get a callback to work. I have the following function call:
memberPhotoPath(dbResults[i].userid2,dbResults[i].userid2Gender,'small',dbResults[i].userid2PhotoName,dbResults[i].userid2PhotoVerified,false,function(path) {
console.log(path);
});
and the following function:
function memberPhotoPath(userid,gender,photoSize,photoName,photoVerification,border,callback) {
if(photoVerification) {
callback('http://www.datingimages.co/online-dating/dating-photos/'+userid+'/'+userid+'-'+photoSize+'-'+photoName+'.jpg');
}else{
if(border) {
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'.png');
}
}else{
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'-noborder.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'-noborder.png');
}
}
}
}
I get the following error in Node.js:
TypeError: undefined is not a function
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:223:21
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:214:9
at Array.forEach (native)
at /etc/node/index.js:208:34
at Query._callback (/etc/node/index.js:287:9)
at Query.Sequence.end (/usr/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
at Query._handleFinalResultPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
Any advice on what I'm doing wrong?
thankyou
I have just run this code in the chrome console and it works fine. The error must be somewhere down the line. With asynchronous functions it is sometimes not possible to track the exact stack trace. I advise you brace yourself and go through your other code again

Resources