Categories
JavaScript Answers

How to get Schema of Node Mongoose database which defined in another model?

To get Schema of Node Mongoose database which defined in another model, we use the schema property.

For instance, we write

const SongSchema = require("mongoose").model("Song").schema;

to call the model method to get the 'Song' model.

And we use the schema property to get its schema.

Categories
JavaScript Answers

How to query for a non-existent (null) attribute in Node DynamoDB?

To query for a non-existent (null) attribute in Node DynamoDB, we calkl the scan method.

For instance, we write

const params = {
  TableName: "Accounts",
  FilterExpression: "attribute_not_exists(email)",
};

dynamodb.scan(params, (err) => {
  if (err) console.log(JSON.stringify(err, null, 2));
  else console.log(JSON.stringify(data, null, 2));
});

to call scan with the params object to make the query.

We query for null value for the FilterExpression field with 'attribute_not_exists(email)'.

Categories
JavaScript Answers

How to run Node npm install without SSL?

To run Node npm install without SSL, we set the registry to the non-secure registry URL.

To do this, we run

npm config set registry http://registry.npmjs.org/

to set the npm registry URL to http://registry.npmjs.org/.

Categories
JavaScript Answers

How to send custom data along with handshake data in Node socket.io?

To send custom data along with handshake data in Node socket.io, we get the handshake data from the socket.manager.handshaken property.

For instance, we write

const socket = io.connect(window.location.origin, {
  query: "loggeduser=user1",
});

io.sockets.on("connection", (socket) => {
  const endp = socket.manager.handshaken[socket.id].address;
  console.log(socket.manager.handshaken[socket.id].query.user);
});

to call connect to make the connection.

Then we listen for the connection event with on.

In the callback, we get the handshake data with socket.manager.handshaken

And we get the query string from the query property.

Categories
JavaScript Answers

How to fix Node already installed but not linked issue on Mac?

To fix Node already installed but not linked issue on Mac, we change the permission of a few folders and run brew link.

To fix this, we

  1. Run sudo chmod 776 /usr/local/lib to change the permission of the /usr/local/lib to make the folder readable, writable and executable.

  2. Run brew link --overwrite node to link Node.

  3. Run sudo chmod 755 /usr/local/lib to change the directory to be readable and executable.