Sunday 19 March 2017

TypeError: database.insert is not a function occurs in Node.js

Up, running and ready for action! When GET method is used, the below output comes but never completes loading... from POSTMAN Why?

Successfully connected to MongoDB instance!
MongoDB returned the following documents:

[ { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'Apple',
    price: 2.5 },
  { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'Pear',
    price: 3 },
  { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'Orange',
    price: 3 } ]

When POST method is used, the below error occurred. Why?

/Users/json/Dev/restful_api/api.js:21
  database.insert('OrderBase', resourceName, resource, function(err, resource) {
           ^
TypeError: database.insert is not a function
    at insertResource (/Users/json/Dev/restful_api/api.js:21:12)
    at insertProduct (/Users/json/Dev/restful_api/api.js:34:3)
    at IncomingMessage.<anonymous> (/Users/json/Dev/restful_api/api.js:66:9)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Can anyone explain? I am new to NodeJs. Thanks a lot!

var http = require('http');
var database = require('./database');
var url = require('url');


// Generic find methods (GET)
function findAllResources(resourceName, req, res) {
  database.find('OrderBase', resourceName, {}, function (err, resources) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resources));
  });
};
var findResourceById = function (resourceName, id, req, res) {
  database.find('OrderBase', resourceName, {'_id': id}, function(err, resource) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resource));
  });
};
// Generic insert/update methods (POST, PUT)
var insertResource = function (resourceName, resource, req, res) {
  database.insert('OrderBase', resourceName, resource, function(err, resource) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resource));
  });
};
// Product methods
var findAllProducts = function (req, res) {
  findAllResources('Products', req, res);
};
var findProductById = function (id, req, res) {
  findResourceById('Products', id, req, res);
};
var insertProduct = function (product, req, res) {
  insertResource('OrderBase', 'Product', product, function (err, result) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(result));
  });
};

var server = http.createServer(function (req, res) {
// Break down the incoming URL into its components
  var parsedURL = url.parse(req.url, true);
// Determine a response based on the URL
  switch (parsedURL.pathname) {
    case '/api/products':
    if (req.method === 'GET') {
    // Find and return the product with the given id
      if (parsedURL.query.id) {
        findProductById(id, req, res);
      }
      // There is no id specified, return all products
      else {
        findAllProducts(req, res);
      }
    }
    else if (req.method === 'POST') {
      //Extract the data stored in the POST body
      var body = '';
      req.on('data', function (dataChunk) {
        body += dataChunk;
      });
      req.on('end', function () {
        // Done pulling data from the POST body.
        // Turn it into JSON and proceed to store it in the database.
        var postJSON = JSON.parse(body);
        insertProduct(postJSON, req, res);
      });
    }
    break;
    default:
    res.end('You shall not pass!');
  }
});

server.listen(8080);
console.log('Up, running and ready for action!');



via iterryh

No comments:

Post a Comment