Categories
JavaScript Answers

How to write to S3 with Node AWS Lambda function?

To write to S3 with Node AWS Lambda function, we use the putObject method.

For instance, we write

const AWS = require("aws-sdk");

const putObjectToS3 = (bucket, key, data) => {
  const s3 = new AWS.S3();
  const params = {
    Bucket: bucket,
    Key: key,
    Body: data,
  };
  s3.putObject(params, (err, data) => {
    if (err) {
      console.log(err, err.stack);
    } else {
      console.log(data);
    }
  });
};

to call putObject to put the file at the Bucket with the Key.

Body has the file data.

And we get the file data from the data parameter in the callback.

Categories
JavaScript Answers

How to catch errors in JavaScript promises with a first level try-catch?

To catch errors in JavaScript promises with a first level try-catch, we use async and await.

For instance, we write

const func = async () => {
  try {
    const asyncResult = someAsyncAction();
    const someValue = await getSomeValue();
    doSomethingWith(someValue);
    await asyncResult;
  } catch (error) {
    console.error(error);
  }
};

to define the func function.

We use await to run the promises and wait for its result.

And we use a catch block to catch any promise rejections.

error would have the rejection reason.

Categories
JavaScript Answers

How to organize routes in Node.js?

To organize routes in Node.js, we move them to their own modules.

For instance, we write

const express = require("express");
const routes = require("./routes");
const user = require("./routes/user");
const http = require("http");
const path = require("path");

const app = express();

app.set("port", process.env.PORT || 3000);

app.get("/", routes.index);
app.get("/users/:id", user.getUser);

http.createServer(app).listen(app.get("port"), () => {
  console.log("Express server listening on port " + app.get("port"));
});

in app.js to add the routes from the other files into the app.

In routes/index.js, we write

exports.index = (req, res) => {
  res.render("index", { title: "Express" });
};

to export the index route.

And we add the getUser route into routes/user.js with

exports.getUser = (req, res) => {
  //...
};
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)'.