First let me say two things: I'm starting with graphDB and I tried this post: Gremlin's valueMap() returns an empty object with JS and Neptune and did not work.
I'm following the same steps as I'm using in the gremlin console. In the console is working 100%.
In gremlin console:
gremlin> g.V().hasLabel('user').has('userUuid', '12345').out('knows').order().by(out('knows').count(), desc).range(0, 20).hasLabel('user').valueMap('userUuid', 'username', 'email')
This query is returning:
Now, the following NodeJs code is not working with the valueMap function.
const value = await g.V().hasLabel(constants.USER)
.has(constants.USER_UUID, '12345')
.out('knows')
.order()
.by(__.out('knows').count(), gremlin.process.order.desc)
.range(pageIndex, pageSize)
.valueMap(constants.USER_UUID, constants.USERNAME, constants.EMAIL)
.toList();
The above code is returning:
Response: [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
Note: if I remove the .valueMap(constants.USER_UUID, constants.USERNAME, constants.EMAIL), the function will return the values as list, instead of mapped into objects for each position inside the list.
What am I doing wrong?
I've found this post on AWS:
And there is a link to this post here on SOF:
Issue with .project().by() in Gremlin JS 3.4.0
This works for me.
Related
I want to create a graph within gremlin-server from a node.js backend with the javascript driver of gremlin. As I added two properties, one id and one username, the id is working, the username is not stored. Here is the code:
const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
const { t: { id } } = gremlin.process;
const { cardinality: { single} } = gremlin.process;
async function createUser(userid,username) {
const vertex = await
g.addV('User')
.property(id,userid)
.property(single,'username',username)
.iterate();
return vertex;
}
await createUser(1001,"testuser")
The output is (when I search the node with g.V(1001).listAll();) The properties are always undefined.
[Vertex { id: 1001, label: 'User', properties: undefined }]
The gremlin server was loaded/run with docker with the following commands:
docker pull tinkerpop/gremlin-server
docker run -d -p 8182:8182 --name gremlin tinkerpop/gremlin-server
The gremlin-driver in node.js has a the version: "gremlin": "^3.4.10",
I've tried with and without the cardinality single above, added more properties, but non is working. The internet searches showed some gremlin-console(groovy) working examples with the .property step, but no hint for the combination node.js-driver of gremlin and the gremlin-server.
I imagine your code is working fine and that the properties are present. The issue is that graph elements returned from queries are "references" only - meaning, they only include id and label and no properties. You should convert your results to use generic containers like Map using a step like elementMap(). You can find more discussion on this in the documentation in various places, but perhaps start with this and if you are interested more in why this is the way it is and what challenges are involved in changing it, please see this.
I have an array declared as carousels: any = [] in my TS file but I keep getting an error stating 'only arrays and iterables allowed' whenever I loop over it using *ngFor. The data inside this array is acquired from mLab.
The goal of my code is to update an object from mLab using its id which seems to work fine. But whenever I press the update, given the fact that the update even works, why do I still get the error?
I have tried to change carousels: any = [] to just carousels = [] but it doesn't seem to change anything.
This is my code.
api.js (backend) - this is the code that communicates with the database.
router.route('carousel/update/:_id).put(function(req, res) {
db.collection('home').updateOne({"_id": ObjectId(req.params._id)}, {$set: req.body}, (err, results) => {
if (err) throw err;
res.send(results)
console.log(req.params._id)
});
});
home.service.ts
return this.http.put<any[]>('./api/carousel/update/' + id, {'header': newheader, 'subheader': newsubheader})
}
component.ts
declaration:
carousels: any = [];
update function:
updateSlide(id: number){
this.HomeService.updateSlide(id, this.header, this.subheader).subscribe(slides => {this.carousels = slides})
}
Update works but I still get the Error: error trying to diff '[object object]'. only arrays and iterables are allowed
The .updateOne({}) doesn't return a list of objects, but an object with the information about the row updated.
You are returning that Object from the API and then setting it in the updateSlider() in your component, where you should be setting an array instead.
Try to log the results in the api or slides in the Subscription, and you should be able to see the issue.
After several readings and posts, I have found out that my mistake was tha I manually subscribed to the Observable inside my component file.
Manually updating looks like this:
.subscribe(slides => {this.carousels = slides})
It turns out that this has already been done automatically therefore I would not need to do it again manually. Refer to this post: Pipe Async - Error trying to diff '[object Object]'. Only arrays and iterables are allowed
This fixed the issue for me.
Problem: Encountered a really weird bug when our service A (laravel php) calls an endpoint by service B (nodejs typescript + ajv + nestjs).
Let me explain it further below.
Example code in service A for building the query parameters
$ids = [1,2,3];
$queryParams = http_build_query(['ids' => $ids]);
dump($queryParams)
// ids%5B0%5D=1&ids%5B1%5D=2&ids%5B2%5D=3
// b.com/bar?ids[0]=1&ids[1]=2&ids[2]=3
In service B, the expected received query params should be
{ ids: [1,2,3] }
It worked all well in dev, code deployed to production, then we started seeing validation errors from SOME of the requests because the query params is now
{
ids: {
'0' : '1',
'1' : '2',
'2' : '3'
}
}
We are pretty sure latest code is deployed to all instances so that rules out possibility of code difference in service A causing the difference in query params sent.
Spent 1 whole day trying to trace the root cause but still no answers at the moment.
Question: What format could the query parameters be sent in in order for it to be parsed into the object structure above?
Turns out the issue is due to nestjs #Query decorator (which uses Express under the hood) being unable to parse the query params correctly into expected array if length of params is above 21.
e.g
Query array with 21 elements
ids[0]=160431133&ids[1]=16048883&ids[2]=16048882&ids[3]=16041333&ids[4]=16041313&ids[5]=16041303&ids[6]=16041293&ids[7]=16041283&ids[8]=16041273&ids[9]=16041263&ids[10]=16041253&ids[11]=16041243&ids[12]=16041233&ids[13]=16041133&ids[14]=19116791&ids[15]=19116691&ids[16]=19116591&ids[17]=19116590&ids[18]=19116581&ids[19]=19116571&ids[20]=12416591&
Parsed as
['160431133', ...., '12416591']
Query array with more than 21 elements
ids[0]=160431133&ids[1]=16048883&ids[2]=16048882&ids[3]=16041333&ids[4]=16041313&ids[5]=16041303&ids[6]=16041293&ids[7]=16041283&ids[8]=16041273&ids[9]=16041263&ids[10]=16041253&ids[11]=16041243&ids[12]=16041233&ids[13]=16041133&ids[14]=19116791&ids[15]=19116691&ids[16]=19116591&ids[17]=19116590&ids[18]=19116581&ids[19]=19116571&ids[20]=12416591&ids[21]=123456789
Parsed as
{
'0': 160431133
.
.
.
'21': 123456789
}
I want to use gremlin-javascript to traverse the remote graph and get a list of vertex, whose id is within a list of predefined ids.
const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const GraphPredicate = gremlin.process.P;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();
const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
g.V()
.has('id', GraphPredicate.within([414, 99999]))
.toList()
.then(function (result) {
console.log(result);
});
Above are the codes I have tried, but it gave me an empty list of vertex, whereas, I expected to have a vertex(414) in the result.
Moreover, when I tested with the gremlin-console by using the statement below, it gave me the vertex(414) in the result.
:> g.V().hasId(within(414,99999))
So I have some questions here:
Do I miss something in the configuration in order to use Predicate?
In the method of javascript GraphPredicate.within([414, 99999])) is the parameter supposed to be only an array of elements or a list of elements separated by a comma as in the case with gremlin-console? By the way, I have tried both ways, but I always got an empty result.
Thank you in advance,
The id is an special property in TinkerPop and can't be retrieved using the name property "id".
The correct way to retrieve by ids in your case should be:
g.V().hasId(P.within(414, 99999)).toList()
.then(result => console.log(result));
Additionally, this can be simplified by removing the within() call:
g.V().hasId(414, 99999).toList()
.then(result => console.log(result));
I can't comment, but in order to use id, you need to use gremlin.process.t.id
Example:
in Gremlin .by(id)
in JS .by(gremlin.process.t.id)
Hope this is helpful
My JSON is:
body =
{
"session_id":"45470003-6b84-4a2b-bf35-e850d1e2df8b",
"message":"Thanks for calling service desk, may I know the store number you are calling from?",
"callstatus":"InProgress",
"intent":"",
"responseStatusCode":null,
"responseStatusMsg":null,
"context":"getstorenumber"
}
How to get message value using Node js? Please let me know after testing.
i tried body.message and body['message'] and also body[0]['message']. I am always getting "undefined"
#Chris comment, since the problem is sorted out, Adding the answer to all members for future ref.
From node.js result, body is taken as JSON string, using JSON.parse.body converted to json object.
body =
{
"session_id":"45470003-6b84-4a2b-bf35-e850d1e2df8b",
"message":"Thanks for calling service desk, may I know the store number you are calling from?",
"callstatus":"InProgress",
"intent":"",
"responseStatusCode":null,
"responseStatusMsg":null,
"context":"getstorenumber"
}
JSON.parse.body
console.log(body.message);