Categories
TypeScript Answers

How to inject Nest.js service from another module with TypeScript?

Sometimes, we want to inject Nest.js service from another module with TypeScript.

In this article, we’ll look at how to inject Nest.js service from another module with TypeScript.

How to inject Nest.js service from another module with TypeScript?

To inject Nest.js service from another module with TypeScript, we’ve to export the module before we can import it.

For instance, we write

@Module({
  controllers: [ItemsController],
  providers: [ItemsService],
  exports: [ItemsService],
})
export class ItemsModule {}

to export the ItemsService with

exports: [ItemsService],

Then in another module file, we write

@Module({
  controllers: [FooController],
  providers: [FooService],
  imports: [ItemsModule],
})
export class FooModule {}

to import the ItemsModule with

imports: [ItemsModule],

Conclusion

To inject Nest.js service from another module with TypeScript, we’ve to export the module before we can import it.

Categories
JavaScript Answers

How to remove first and last slash with a JavaScript regular expression?

Sometimes, we want to remove first and last slash with a JavaScript regular expression.

In this article, we’ll look at how to remove first and last slash with a JavaScript regular expression.

How to remove first and last slash with a JavaScript regular expression?

To remove first and last slash with a JavaScript regular expression, we can use the /^\/|\/$/g regex.

For instance, we write

const newString = theString.replace(/^\/|\/$/g, "");

to call theString.replace with the /^\/|\/$/g regex to replace all matches in the theString string with empty strings.

^\/ matches the starting slash.

\/$ matches the ending slash.

| combines the 2 patterns together with OR.

g makes the regex match all instances of the patterns.

Conclusion

To remove first and last slash with a JavaScript regular expression, we can use the /^\/|\/$/g regex.

Categories
JavaScript Answers

How to create object using variables for property name with JavaScript?

Sometimes, we want to create object using variables for property name with JavaScript.

In this article, we’ll look at how to create object using variables for property name with JavaScript.

How to create object using variables for property name with JavaScript?

To create object using variables for property name with JavaScript, we can use the computed property name syntax.

For instance, we write

const create = (propertyName) => {
  const myObject = { [propertyName]: "value" };
  return myObject;
};

to define the create function that takes the propertyName string parameter.

In it, we use the return value of propertyName as the property name.

And we set its value to 'value'.

Conclusion

To create object using variables for property name with JavaScript, we can use the computed property name syntax.

Categories
JavaScript Answers

How to paste an image from clipboard using JavaScript?

Sometimes, we want to paste an image from clipboard using JavaScript.

In this article, we’ll look at how to paste an image from clipboard using JavaScript.

How to paste an image from clipboard using JavaScript?

To paste an image from clipboard using JavaScript, we can listen to the document‘s paste event.

For instance, we write

document.onpaste = (event) => {
  const items = (event.clipboardData ?? event.originalEvent.clipboardData)
    .items;
  for (const item of items) {
    if (item.kind === "file") {
      const blob = item.getAsFile();
      const reader = new FileReader();
      reader.onload = (event) => {
        console.log(event.target.result);
      };
      reader.readAsDataURL(blob);
    }
  }
};

to set document.onpaste to a function that runs when we paste something onto the page.

In it, we get the pasted items from event.clipboardData or event.originalEvent.clipboardData.

Then we use a for-of loop to loop through the items.

In it, we check if item.kind is a 'file'.

If it is, we call item.getAsFile to get the pasted item as a blob.

And we use the FileReader instance’s onload method get the read file.

The read file’s content is in event.target.result.

It’s a base64 string since we read it with readAsDataURL.

Conclusion

To paste an image from clipboard using JavaScript, we can listen to the document‘s paste event.

Categories
JavaScript Answers

How to prevent SQL injection in Node.js?

Sometimes, we want to prevent SQL injection in Node.js.

In this article, we’ll look at how to prevent SQL injection in Node.js.

How to prevent SQL injection in Node.js?

To prevent SQL injection in Node.js, we can use parameterized queries.

For instance, we write

const userId = 5;
const query = connection.query(
  "SELECT * FROM users WHERE id = ?",
  [userId],
  (err, results) => {
    // ...
  }
);

to call the Node mysql package’s connection.query method with a string that has the ? placeholder for the id value.

The 2nd argument is an array with the values to fill the placeholders with.

And the last argument is a callback with the results returning the query results.

userId will be escaped before the query is made so eliminate the risk of SQL injection.

Conclusion

To prevent SQL injection in Node.js, we can use parameterized queries.