I am using Orientjs to access a Orientdb database from a nodejs script, but the server side function does not effect the database.
Server side script:
This Javascript function addTxt() takes the argument author and text
var db = orient.getGraph();
db.command('sql','insert into Message (author, text) VALUES ("'+author+'", "'+text+'")');
return "1";
Query: This function has been tested in Orient Studio and the following query works:
SELECT addTxt("Testuser","foo")
Nodejs/Orientjs: When invoking this function from a nodejs script using Orientjs, it only returns
[ { '#type': 'd', addTxt: '1', '#rid': { cluster: -2, position: 1 } } ]
and the database remains untouched.
I have tried:
//some code
var OrientDB = require('orientjs');
var server = OrientDB({
host: 'localhost',
port: 2424,
});
var db = server.use({
name: 'database',
username: 'admin',
password: 'pass'
db.query('SELECT addTxt(:arg1, :arg2)', {
params: {arg1:"Testuser",arg2:"foo"}
}).then(function (response){
console.log(response);
});
Other queries from Orientjs works.
What am I doing wrong? Is there an other way to invoke a server side function?
You explicit returns "1"
it is right that returns
[ { '#type': 'd', addTxt: '1', '#rid': { cluster: -2, position: 1 } } ]
try to directly explicit the commit in your function
db.commit()
Related
I'm using node-opcua to create an opc-ua server using node.js.
On the server-side, I register a method that has an 2-Dimensional Array as an input argument. When browsing the server from a client like UaExpert, I see the method with a proper descriptions of the arguments. I can set the argument-values from the client, but calling the method just fails with a BadInvalidArgument exception.
As soon as I change the dimension of the array to 1 (by setting valueRank to 1), everything works as expected.
I'm following the minimal sample from node-opcua on server-methods:
const {
OPCUAServer,
DataType,
Variant,VariantArrayType,
StatusCodes,
makeAccessLevelFlag
} = require("node-opcua");
(async () => {
try {
const server = new OPCUAServer({
port: 4334 // the port of the listening socket of the server
});
await server.initialize();
const addressSpace = server.engine.addressSpace;
const namespace = addressSpace.getOwnNamespace();
const myDevice = namespace.addObject({
organizedBy: addressSpace.rootFolder.objects,
browseName: "MyDevice"
});
//_"adding a method on the device object"
const method = namespace.addMethod(myDevice,{
browseName: "Bark",
inputArguments: [
{
name:"nbBarks",
description: { text: "specifies the number of time I should bark" },
dataType: DataType.Int32,
valueRank: 2,
arrayDimensions: [2, 2]
}
],
outputArguments: [{
name:"Barks",
description:{ text: "the generated barks" },
dataType: DataType.UInt32,
valueRank: -1
}]
});
//_"binding the method with your own function"
method.bindMethod((inputArguments,context,callback) => {
const callMethodResult = {
statusCode: StatusCodes.Good,
outputArguments: [{
dataType: DataType.String,
arrayType: VariantArrayType.Array,
value :99
}]
};
callback(null,callMethodResult);
});
await server.start();
console.log("Server is now listening ... ( press CTRL+C to stop)");
const endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log(" the primary server endpoint url is ", endpointUrl );
}
catch(err) {
console.log(err);
}
})();
Here is a screenshot before calling the method from UaExpert:
It seems the client correctly recognizes the argument as an array with the dimension [2x2]. But when calling the method, I just get a BadInvalidArgument exception on the client-side.
Any hints what I'm doing wrong?
It's probably a bug in the node-opcua stack.
I have two server-side applications editing the same database.
One server is a feathersjs app and another is a simple nodejs app. A frontend app connects to the feathersjs app via feathersjs client.
How, when the nodejs app edits the database, can I update clients connected to the feathersjs app? As currently any changes made outside the featherjs app aren't reflected on the feathersjs clients.
Can I trigger the patched event somehow and force the clients to pull down the updated data?
if you are using mongodb with WiredTiger storageEngine you can use the collection.watch() function and add a monitor in your feathers app something like this
//src/services/monitor.js
module.exports = function(app){
//get mongo client
const mongoClient = app.get('mongoClient');
//connect db
mongoClient.then(db => {
//collection to watch
const collection = db.collection('your_collection_name')
//watch collection
const changeStream = collection.watch({ fullDocument: 'updateLookup' });
//on some data changed
changeStream.on('change', data => {
console.log ( 'something is changed in your collection' , data )
//your service.emit
});
})
}
Then I added this simple monitor in the /src/services/index.js (maybe not the right way but it works)
//src/services/index.js
...
const monitor = require('./monitor.js');
module.exports = function (app) {
...
app.configure(monitor);
...
};
Data returned on every change on the collection
{ _id:
{ _data:
'825C7F03230000001C29295A100490DEF2C65812410FABF0DE46F9C89D7246645F696400645C1AC097B189CBD5D4A24D330004' },
operationType: 'replace',
clusterTime:
Timestamp { _bsontype: 'Timestamp', low_: 28, high_: 1551827747 },
fullDocument:
{ _id: 5c1ac097b189cbd5d4a24d33,
id: '12',
language: 'it-IT',
category: 'some data',
slug: '',
description: 'some data',
src:'',
color: 'card',
status: true,
home: true,
order_int: '3',
visual: 'card' },
ns: { db: 'mydb', coll: 'mycollection' },
documentKey: { _id: 5c1ac097b189cbd5d4a24d33 } }
More info here https://docs.mongodb.com/manual/reference/method/db.collection.watch/
As you pointed out, only changes made through the Feathers API will be reflected but on the server you can always emit the event you need via service.emit:
dbConnection.on('someDatabaseUpdate', data => {
app.service('messages').emit('patched', data);
app.service('messages').emit('updated', data);
});
Things to note here (also discussed in this issue):
There will be no user or any other information about the method call
data will not be run through any service hooks
I have an Azure Functionapp that processes some data and pushes that data into an Azure servicebus topic.
I require sessions to be enabled on my servicebus topic subscription. I cannot seem to find a way to set the session id when using the javascript functionapp API.
Here is a modified extract from my function app:
module.exports = function (context, streamInput) {
context.bindings.outputSbMsg = [];
context.bindings.logMessage = [];
function push(response) {
let message = {
body: CrowdSourceDatum.encode(response).finish()
, customProperties: {
protoType: manifest.Type
, version: manifest.Version
, id: functionId
, rootType: manifest.RootType
}
, brokerProperties: {
SessionId: "1"
}
context.bindings.outputSbMsg.push(message);
}
.......... some magic happens here.
push(crowdSourceDatum);
context.done();
}
But the sessionId does not seem to get set at all. Any idea on how its possible to enable this?
I tested sessionid on my function, I can set the session id property of a message and view it in Service Bus explorer. Here is my sample code.
var connectionString = 'servicebus_connectionstring';
var serviceBusService = azure.createServiceBusService(connectionString);
var message = {
body: '',
customProperties:
{
messagenumber: 0
},
brokerProperties:
{
SessionId: "1"
}
};
message.body= 'This is Message #101';
serviceBusService.sendTopicMessage('testtopic', message, function(error)
{
if (error)
{
console.log(error);
}
});
Here is the test result.
Please make sure you have enabled the portioning and sessions when you created the topic and the subscription.
I have this script in php that opnes a web service url to send an sms
<?php
$amount = 300;
$url = 'http://sms.com.co/webservice/sms.php?method=Submit&account=adam&password=123456&mobile=773839&content=helloworld;'
echo file_get_contents($url);
?>
In nodejs i have this
var urllib = require('urllib');
urllib.request('http://sms.com.co/webservice/sms.php?method=Submit&account=adam&password=123456&mobile=773839&content=helloworld');
I am looking for a function or package in meteor that i can use so that i can avoid installing the urllib npm package.
You can use "HTTP" for this.
HTTP.call('get', 'http://sms.com.co/webservice/sms.php', {
params: {
method: 'Submit',
account: 'adam',
password: '123456',
mobile: '773839',
content: 'helloworld'
}}, function(err, res) {
// do stuff
})
Upon calling this from your Meteor method will be asynchronous. Your method will not wait for the response from the HTTP call. In order to do so you need to use wrapAsync like this:
var convertAsyncToSync = Meteor.wrapAsync(HTTP.get),
apiCall = convertAsyncToSync(yourURLHere, {params});
if (apiCall.statusCode === 200) {
// do stuff
}
Sounds like you need "HTTP" from the Meteor core libraries. See http://docs.meteor.com/api/http.html for details. Install it using:
meteor add http
This lets you open a URL using either a method or without a method from the server:
HTTP.call('get', 'http://sms.com.co/webservice/sms.php', {
params: {
method: 'Submit',
account: 'adam',
password: '123456',
mobile: '773839',
content: 'helloworld'
}}, function(err, res) {
// do stuff
})
I have a function called UpdateDocument.js and I have to run this function when I do request. It's only running once. After first request, it's not updating document. I have to refresh page but this is not why I want.
I'm requesting from client side and running function in server side.
socket.emit('UpdateDocumentRequest', ....);
UpdateDocument.js :
var users = require('./datas/user');
exports.UpdateDocument = function(app, passport, io) {
io.on('connection', function(socket){
socket.on('UpdateDocumentRequest', function(data) {
//console.log(data);
socket.emit('UpdateDocumentRequest__return', { last_balance: last_balance });
var query = {....};
users.update(query, { ...}, function (err, statu) {
});
});
}
In server.js
UpdateDocument.UpdateDocument(app, passport, io);
Console :
As you can see, they have different dates.
{ user: '',
btc_balance: '0.00008630',
betAmount: '0.00000002',
betPayout: '2',
betProfit: '0.00000002',
betTime: '2014-08-24T05:17:45.328Z' }
{ user: '',
btc_balance: '0.00008630',
betAmount: '0.00000002',
betPayout: '2',
betProfit: '0.00000002',
betTime: '2014-08-24T05:17:47.225Z' }
{ user: '',
btc_balance: '0.00008630',
betAmount: '0.00000002',
betPayout: '2',
betProfit: '0.00000002',
betTime: '2014-08-24T05:17:49.871Z' }