The official documentation for assert lists many more methods than I am seeing in NodeJS 13.2.
const assert = require('assert');
assert.match('f01', /.\d\d/); // Exception 'TypeError: assert.match is not a function'
Why are things like match() missing?
The match method is missing in Node.js v13.2. You can see the documentation for version 13.2 here.
Node.js no longer supports assert.match(). Sorry!
A quick fix is to use the regex.test(), together with assert.ok() like this:
let html = "<html><body><h1>Welcome</h1>...</body></html>";
assert.ok(/<h1>Welcome<\/h1>/i.test(html), "Incorrect title");
For your example, try this:
assert.ok(/.\d\d/.test('f01'));
Related
I'm a NodeJS beginner and I'm trying to create a protobuf object in NodeJS but I don't seem to understand the concept yet.
The .proto file I'm working with is here: https://github.com/meshtastic/Meshtastic-protobufs/blob/ab16c249dd5ed99a26ee3fe76ec84808d53d791a/mesh.proto#L889
My code looks like this:
[…]
const root = protobuf.loadSync('Meshtastic-protobufs/mesh.proto');
const toRadio = root.lookupType('ToRadio');
[…]
const message = toRadio.create({
want_config_id: 12345678
});
const buffer = toRadio.encode(message).finish();
console.log(util.inspect(toRadio.decode(buffer)));
But apparenty, no protobuf object is created. The debug output from the console.log format shows ToRadio {}.
I'm looking for somewhat of a "protobufs for Dummies" explanation on how to read and use the proto files so that I can properly create objects.
Have a look at the quickstart and tutorial on the gRPC web site:
https://grpc.io/docs/languages/node/
Oh well, turns out, the parameter names in Node actually differ from the parameter names in the .proto file.
e.g. "want_config_id" in the proto file changes to "wantConfigId" in Node.
The following example does not work for me in Node.js using the 'gremlin' 3.4.1 npm package:
g.V().has('person','name','bill').tryNext().orElseGet{g.addV('person').property('name','bill').next()}
I am getting a TypeError saying tryNext() is not a function. What am I doing wrong?
import {driver, structure} from 'gremlin';
import DriverRemoteConnection = driver.DriverRemoteConnection;
import Graph = structure.Graph;
const g = new Graph().traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
console.log(g.V().toList()); <= working
Now using the line from above in that code will not work, but it does work using the Gremlin console.
Trying to call a function that doesn't exist, which appears to be as stated in the Gremlin docs, to wit:
tryNext() will return an Optional and thus, is a composite of hasNext()/next() (only supported for JVM languages).
http://tinkerpop.apache.org/docs/current/reference/#terminal-steps
Caveat: Never used TinkerPop, never used Gremlin. But I know how to use the web. Could be this is wrong, but the docs do seem fairly clear.
If tryNext() not supported as Dave mentioned.
You can rewrite your query to do the same with other gremlin steps:
g.V().has('person','name','bill').fold().coalesce(unfold(),g.addV('person').property('name','bill')).next()
When I first used MongoDB I managed to connect successfully, however then I wanted to carry out the most basic query such as:
db.users.find()
I got an error saying TypeError: Cannot read property 'find' of undefined
Basically meaning I cannot use a collection as a property to the object db.
So i tried this:
var user_col = db.collection('users');
user.col.find();
which works absolutely fine.
Over the last few days I have kept having to look up other ways of doing things as the standard documented way doesn't seem to work. Just now I wanted to get the total users on the app, so like it says in the documentation I should do this:
var count = db.runCommand( { count: 'users' } );
console.log(count);
however this gave the error:
TypeError: undefined is not a function
Is there a problem with MongoDB you have seen like this before or am I just being stupid? I do not want to have to keep finding other, less efficient ways of doing things so finally I ask here what is going on.
Thank you.
It appears you are confusing the Mongo shell API with the node.js native driver API. While both are JavaScript, the shell is sync while node.js is async so they're totally different.
I installed validator module via npm.I am loading the module via this code:
var check = require('validator').check,
sanitize = require('validator').sanitize;
It loads perfectly but when I run this line
data.message = sanitize(data.message).escape();
I am getting "TypeError: undefined is not a function" for this line.How can I resolve it ?
Found this library that you might want to use instead:
https://www.npmjs.org/package/google-caja
https://github.com/superkhau/node-google-caja
I read that you need specifically an xss filter. So, directly from the github main page of Validator:
XSS Sanitization
XSS sanitization was removed from the library in 2d5d6999.
For an alternative, look at Yahoo's xss-filters library.
From xss-filters tutorial
Firstly, import:
var xssFilters = require('xss-filters');
Then escape it:
data.message = xssFilters.inHTMLData(data.message);
I'm thoroughly confused on how to use an npm module in Meteor client code.
I understand modules like fs would only work server-side, but in this case I'd like to use a simple text module like this for displaying pretty dates:
https://github.com/ecto/node-timeago
I've tried installing the module under /public/node_modules,
and it works great on the server-side following these instructions from SO: (
How do we or can we use node modules via npm with Meteor?)
Meteor.startup(function () {
var require = __meteor_bootstrap__.require
var timeago = require('timeago')
console.log(timeago(new Date()))
...
However it doesn't work in the client-side code:
if (Meteor.is_client) {
var require = __meteor_bootstrap__.require
var timeago = require('timeago')
console.log(timeago(new Date()))
...
Uncaught ReferenceError: __meteor_bootstrap__ is not defined"
Server-side is sort of useless for me in this case, as I'm trying to render text on the client.
I don't believe you need to use the server side version. Use the npm stuff for server side only and btw, put it in your /public/ as well. Who knows maybe you can call it once it is in your /public/, try it. Or try this.
Use something like the jquery timeago.js
Put it in /client/ or something like /client/js
Create a /client/helpers.js or some such.
Use a handlebars helper.
Handlebars.registerHelper('date', function(date) {
if(date) {
dateObj = new Date(date);
return $.timeago(dateObj);
}
return 'a long long time ago in a galaxy far away';
});
Example of calling 'date' handlebars helper function from template.
{{ date created }}
Where date is the handebars helper and created is the date coming out of the meteor/mongo collection.
See the github Britto project. That is where I got this code snippet and used it in a chat room app I wrote. Works fine.
There are a couple of others floating out there. Go to madewith.meteor.com and peruse the source of some of the projects.