How to resolve sequelize-JSON.parse error in Node


When I start with Sequelize ORM end up with the body-parser error. Usually we define body parser using app.use(bodyparser.json()), which globally declare parser functionality for all routes.

SyntaxError: Unexpected token in JSON at position 0
at JSON.parse ()
at createStrictSyntaxError (E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\types\json.js:158:10)
at parse (E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\types\json.js:83:15)
at E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:224:16)
at done (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (node:events:388:22)
at endReadableNT (node:internal/streams/readable:1305:12)
at processTicksAndRejections (node:internal/process/task_queues:80:21)

Here is where sequelize findAll() like functionality collides and the above error log will be populated

Solution

We can apply route specific parser to fix this mess. Let’s see how

Before the fix

For simplicity I have omitted some of the lines in index.js (Node project file). Before the fix our routes look like this

...
app.use(bodyParser.urlencoded({extended:false})) 
app.use(bodyParser.json())

app.post("/todo",  (req, res, next) => {
    const {item,description}=req.body
    Todo.create({item:item,description:description})
    .then((model) => {
      res.status(200).send(model);
      })
      .catch((e) => {
        res.status(400).send("Error:" + e);
      });
  });

app.get("/todo", (req, res, next) => {
  Todo.findAll()
    .then((model) => {
        res.json({
            error: false,
            data: model
        })
    })
    .catch(error => res.json({
        error: true,
        data: [],
        error: error
    }))
});


After the fix

After the removal of the global json parser it will look like this

...
app.use(bodyParser.urlencoded({extended:false})) 
 

app.post("/todo", bodyParser.json(), (req, res, next) => {
    const {item,description}=req.body
    Todo.create({item:item,description:description})
    .then((model) => {
      res.status(200).send(model);
      })
      .catch((e) => {
        res.status(400).send("Error:" + e);
      });
  });

app.get("/todo", (req, res, next) => {
  Todo.findAll()
    .then((model) => {
        res.json({
            error: false,
            data: model
        })
    })
    .catch(error => res.json({
        error: true,
        data: [],
        error: error
    }))
});


we have added the parser function of the bodyParser to Post routes only and the problem solved

How to populate multiple MongoDB objects on the same path in express-node


In Nodejs app we are using mongoose driver for MongoDB, a NoSql database with speed and accuracy. Mongo store data as series of JSON element call document and the collection of document is called a collection , same as table in DBMS.

According to the Mongoose doc Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s)

So we have the following Document , in which we want to populate user information . For simplicity I skipped some of the mongo

[
    { "tags": [
            "5fef1f5187a69346300003a8",
            "5fef1f5187a69346300003a9",
            "5fef1f5187a69346300003aa"
        ],
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": "5fe4ab1e69b8d525c44ec293",
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

The document contains a list of posts with user information etc within the Tag ( not listed) . So the posts is a list of objects which should populate and inside each post have a user and list of tags object as well. Our task is to primarily populate the user object and the tags list.

We can do this right now using the find method of modal class accompanied by the populate command as follows

The tags array and the user belong to the posts path , so we have to repeat the population ( one for user and other for tags)

	const tid = req.params['tid']
			Tag.findById(tid).populate({
				path: 'posts',
				populate: {
					path: 'tags'
				},
				populate: {
					path: 'user',
					select: 'name'
				}
			}).populate({
				path: 'posts',
				populate: {
					path: 'tags',
					select: 'tagName'
				}
			}).exec((err, post) => {
				if (err) return handleError(err)
				console.log('tagged posts are ' + post);
				return res.status(200).send(post.posts)


			})

Result

[
    {
       
        "tags": [
            {
                "_id": "5fef1f5187a69346300003a8",
                "tagName": "Test"
            },
            {
                "_id": "5fef1f5187a69346300003a9",
                "tagName": "VM"
            },
            {
                "_id": "5fef1f5187a69346300003aa",
                "tagName": "JS"
            }
        ],
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": {
            "_id": "5fe4ab1e69b8d525c44ec293",
            "name": "admin"
        },
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

In the above code we had used the populate inside another populate because the each posts had user element.

Following mongoose post also deserve a good read

How to populate selected fields in a mongo document path in expressjs


In Express-Nodejs app we are using mongoose driver for MongoDB, a NoSql database with speed and accuracy. Mongo store data as series of JSON element call document and the collection of document is called a collection , same as table in DBMS.

According to the Mongoose doc Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s)

So we have the following Document , in which we want to populate user information

[
    {
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": "5fe4ab1e69b8d525c44ec293",
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

So the posts is a list of objects which should populate and inside each post have a user object as well. Our task is to primarily populate the specific user object fields.

We can do this right now using the find method of modal class accompanied by the populate command which specify the path and fields to populate as follows, also used a callback function.

const tid = req.params['tid']
			Tag.findById(tid).populate({
				path: 'posts',
				populate: {
					path: 'user',select:'name'
				}
			}).exec((err, post) => {
				if (err) return handleError(err)
				 
				return res.status(200).send(post.posts)


and the response will be as follows

[
    {
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": {
            "password": "123",
            "posts": [
                "5feebf2b7e30080468ccd4c1",
                "5feec08a7e30080468ccd4c9",
                ]
            "_id": "5fe4ab1e69b8d525c44ec293",
            "name": "admin",             
        },
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

In the above code we had used the populate inside another populate because the each posts had user element.

We can use populate inside another one , find out how ? Next post will be on that !

Following mongoose post also deserve a good read

How to populate and display nested mongo objectin express-node route


In Nodejs app we are using mongoose driver for MongoDB, a NoSql database with speed and accuracy. Mongo store data as series of JSON element call document and the collection of document is called a collection , same as table in DBMS.

According to the Mongoose doc Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s)

So we have the following Document , in which we want to populate user information

[
    {
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": "5fe4ab1e69b8d525c44ec293",
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

The document contains a list of posts with user information etc within the Tag ( not listed) . So the posts is a list of objects which should populate and inside each post have a user object as well. Our task is to primarily populate the user object.

We can do this right now using the find method of modal class accompanied by the populate command as follows

const tid = req.params['tid']
			Tag.findById(tid).populate({
				path: 'posts',
				populate: {
					path: 'user'
				}
			}).exec((err, post) => {
				if (err) return handleError(err)
				 
				return res.status(200).send(post.posts)


and the response will be as follows

[
    {
        "_id": "5fef1f5087a69346300003a7",
        "title": "Title post",
        "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
        "user": {
            "password": "123",
            "posts": [
                "5feebf2b7e30080468ccd4c1",
                "5feec08a7e30080468ccd4c9",
                ]
            "_id": "5fe4ab1e69b8d525c44ec293",
            "name": "admin",
            "createdAt": "2020-12-24T14:52:14.364Z",
            "updatedAt": "2021-01-03T02:44:41.780Z",
            "__v": 108
        },
        "createdAt": "2021-01-01T13:10:40.468Z",
        "updatedAt": "2021-01-03T02:37:55.982Z",
        "__v": 11
    }
]

In the above code we had used the populate inside another populate because the each posts had user element.

Following mongoose post also deserve a good read

How to create document using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

MongoDB

MongoDB is a NoSQL database which store data as JSON document and collections. In JavaScript applications we can utilize the mongoose driver to perform Mongo operations. First up all we need to install mongoose to your project and then import the module.

npm install mongoose

Saving the document

mongoose uses the modal class to perform REST operations. If you are not familiar have a look at the mongoose modal post given below

Tag.create({
tagName: tagNa}, function(error, stag) {
if (error) return handleError(error)
 console.log(new tag saved : ${stag.tagName});
})

The Tag is the Modal and the create method is used to save the document to database and it also use a callback function to return the result.

We can also use an asynchronous method find documents in Mongoose as follows

const tag=new Tag({tagName:'SQL'})
await tag.save()

That’s it. Following mongoose posts deserve your attention

How to create Modal for CURD using Mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

Modal

Mongoose allow us to perform CURD operation using REST full Modals. A Modal is basically is an object/class which define all fields with validation and also connected to your MongoDB.

To perform operation ,create variables of the modal and call the save/ create method. Following is a sample Modal class

const mongoose = require('mongoose');
 const UserSchema = new mongoose.Schema({
     name :{
         type:String,
         required: '{PATH} is required!'
     },
     bio: {
         type:String
     },
     email:{
         type:String
     },
     website:{
         type:String
     },
     password:{type:String,required: '{PATH} is required!',default:"123"},
 posts : [     {type: mongoose.Schema.Types.ObjectId,ref:'Post'} ]
 },{
     timestamps: true
 })
 module.exports = mongoose.model('User',UserSchema);

In the modal class you can store another document using ObjectID .Here is an example for the create document using the modal class

const user = await User.create({
             name,
             email,
             bio,
             website
         })

That’s it. Following mongoose posts deserve your attention

How to find document byId using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

Mongo Object Id

MongoDB object ID is an automatically generated multipurpose number. In our case we can use it to show up the document. In fact the Object ID consist of date and a unique random number.

Find records using MongoDB ObjectId

  Post.findById(pid,function (error, post) {
       if (error) return handleError(error)
          if (post) {
               console.log("Full post :" + post);
          }
        });  

Using the findById method of mongoose model class allows us to find the document using MongoDB object Id and return result to the call back function in JavaScript style.

We can also use an asynchronous method find documents in Mongoose as follows

const post=await Post.findById(id)

and the resulted document will be

That’s it. Following mongoose posts deserve your attention

How to create express-routes using controllers


Express is a Nodejs frame work which can be used to create web applications. A route represents a page in the web app or website. An ideal express app consist of multiple web pages.

Prerequisites

We need the following before proceeding to the next step

  • Nodejs, install the Node
  • A Code editor of your choice ( sublimText/Atom/VS-Code)
  • A browser

For simple project we can put all of them inside a single file such as inex.js but it is a good practice to modularize the project with folder and files. So that it help manage the project easily.

The Folder structure

Following are the folder structure for the express project. First create project folder using command prompt or you can use the code editors command

In Atom you can use Shit +A for new folder , A for new file

Project Folder Structure
md express-route
cd express-route
md app
cd app
md controllers

Generate package file for the project with start script. We can use the npm init command for this purpose.

npm init -y 

by default init command will ask you a series of question , we can avoid them and use default values using the -y option.

Also add a start script to the pacjage.json file as follows

  "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "start": "node index.js"
},

Create index,controller,routes files

Index.js : This file is the starting point of our project, create a new files with .js under the Project folder

controller.js : controllers hold the definition for the different routes ( Save under the controllers folder)

routes.js : This file where we declare all our express-routes and later use them in index.js (Save under the app folder)

Install the Express

Lets install essential packages for our project , on the shell please issue the following ( make sure you have internet connection)

npm install express --save

Define the controllers

The controller function is a simple java script module consisting of the following (in the file controllers.js)

module.exports = {
home:(req,res)=>{
  try {
    res.status(200).send('This is a Home Page')
  } catch (e) {
    console.log(e);
  }
},
about:(req,res)=>{
  try {
    res.status(200).send('This is a About Page')
  } catch (e) {
    console.log(e);
  }
},
contact:(req,res)=>{
  try {
    res.status(200).send('This is a Contact Page')
  } catch (e) {
    console.log(e);
  }
},
}

Prepare the routes

Let’s create our express-routes using the controller functions ( in the file app/routes.js )

const express = require('express');
const router = new express.Router;
const Controllers = require('./Controllers/Controllers')

router.get('/',Controllers.home)
router.get('/about',Controllers.about)
router.get('/contact',Controllers.contact)

module.exports = router;

Using express-router module we define the routes for our app and also import and utilized the controller functions and finally exported the module and get things ready for the routes in the index.js

Setup server, routes

The final step is to create use the routes and start our Node server (localhost). Now the index.js will look like

const express= require('express')
const app=express()
//routes
app.use(require('./app/routes'))

app.listen(3000, () => console.log('server on!'));

Here we had imported the routes.js as middle ware and will cause implementing all the routes defined in the routes.js file

Run the server

We can run the project using the start script configured in the package file.

npm start

and in the console the server on message will shown. Got to your browser and lunch the app using http://localhist:3000 and also try /about/ and /contact routes.

How to place list of objects in MongoDB-Express app


With mongoose we can easily place list of objects and populate documents/objects in MongoDB.

The Modal objects

In the modal object of Post we can define a field for user which is another modal in our project.

The User Schema and ‘post’ list field

const mongoose = require('mongoose');
 const UserSchema = new mongoose.Schema({
     name :{
         type:String,
         required: '{PATH} is required!'
     }, 
     posts : [
         {type: mongoose.Schema.Types.ObjectId,ref:'Post'}
     ]    
 ,{
     timestamps: true
 })
 module.exports = mongoose.model('User',UserSchema);

The Post Schema

const mongoose = require('mongoose');
 const PostSchema = new mongoose.Schema({
     title: {
         type: String,
         required:   '{PATH} is required!'
     },
     content: {
         type: String,
         required:   '{PATH} is required!'
     },
 }, {
     timestamps: true
 })
 module.exports = mongoose.model('Post', PostSchema);

In the user we had defined a field posts (list ), which is intended to store the user posts object ( MongoDB object ID), which can be used for populating the post later.

In the express route method we have the postsByUser which fetch the user post in the user documents

 postsByUser: async (req, res) => {
        try {
           const {  id  } = req.params;
        console.log('User:'+id);
        const user = await User.findById(id).populate('posts');
        console.log('User ->' + user);
        res.send(user.posts);
         } catch(e) {
             // statements
             console.log(e);
             res.sta(401).send({error:e})
         } 
    }

The the route is called in the following manner

http://localhost:3005/user/find/post/5fe4ab1e69b8d525c44ec293

and the user posts will be shown as following

{
      "posts": [
        {
            "comments": [],
            "tags": [],
            "_id": "5fedcb477563625708ca7429",
            "title": "Welcome to react",
            "content": "A hello world Reactjs app",
            "user": "5fe4ab1e69b8d525c44ec293",
            "createdAt": "2020-12-31T12:59:51.559Z",
            "updatedAt": "2020-12-31T12:59:51.559Z",
            "__v": 0
        },
        {
            "comments": [],
            "tags": [],
            "_id": "5fedcbe67563625708ca742a",
            "title": "Helo",
            "content": "Hello word app",
            "user": "5fe4ab1e69b8d525c44ec293",
            "createdAt": "2020-12-31T13:02:30.724Z",
            "updatedAt": "2020-12-31T13:02:30.724Z",
            "__v": 0
        },   
}          

That’s it. Following mongoose posts deserve your attention

Use Object id to set relationship in MongoDB-Express app


Unlike relational database NoSql (MongoDB) don’t have relation between documents. Even though we can set relation by placing sub document inside one document using the Object id, which is generated by the MongoDB.

The Modal objects

In the modal object of Post we can define a field for user which is another modal in our project.

Continue reading Use Object id to set relationship in MongoDB-Express app