Using http query string as a database object node.js/express

Kevin Bradshaw

Experimenting with node.js / express / mongodb.

I'm using

http://localhost:3000/models/save?model={"name":"blah blah blah"}

to pass a test JSON object to the express route /models/save for saving to the mongodb. Everything works great except the collection.insert statement which returns an "undefined" error.

I think it must be that the parameter extracted from the query string by var model = req.query.model; is not in the correct format. Any ideas?

The code is as follows:

var express = require('express');
var router = express.Router();

router.get('/save', function(req, res) {

// Set our internal DB variable
var db = req.db;

var model = req.query.model;
console.log (model);

// Set our collection
var collection = db.get('models');

// Submit to the DB
collection.insert ( model, function (err, doc) {
    if (err) {
        // If it failed, return error
        res.send("There was a problem saving to the database.");
        console.log(doc);
    }
    else {
        console.log ("model saved");
        res.send("OK")
    }
});
});
module.exports = router;
user2941651

Please try the following:

var model = JSON.parse(req.query.model);

instead of the line

var model = req.query.model;

JSON.parse method parses JSON string representation (which in your case is model request query parameter) and returns Javascript object which you can then use in your insert process.

I hope it helps some way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using node-couchdb to query a database

From Dev

How to find documents in database using query string

From Dev

Navigate using router and pass an object as a query string

From Java

node.js http 'get' request with query string parameters

From Dev

Query Cloudant (using the node.js module) database for a list of documents?

From Dev

Passing a query string to a request as an actual string using Node and Express

From Dev

How to store emoji string in Database using Laravel with Orm Query

From Dev

Replace string in all columns of a SQLite database using one simple query

From Dev

How do I save a string from database query using codeigniter

From Dev

HTTP DELETE using jQuery ajax with parameters in query string

From Dev

HTTP DELETE using jQuery ajax with parameters in query string

From Dev

Query string is appending when using $_SERVER['HTTP_REFERER']

From Dev

Node - Database query callback not firing

From Dev

using node.js how to find the returned value is string or [object]

From Dev

Struggling to translate jQuery JSONP query to a Node.JS query using http.request

From Dev

Using a function to query a database

From Dev

Convert Query String to Object

From Dev

Changing SQL string query to parametized query; syntax for calling database class file using C#

From Dev

How to execute a mongo query string on a node server using native node driver?

From Dev

Database query not returning full string

From Dev

Why am I apparently connecting to SQL Azure database using node,js but query is not executed?

From Dev

Negative selection http query string (IS NOT)

From Dev

HTTP Caching URLs with Query String

From Dev

bash - parse a http query string

From Dev

Not using database connection string

From Dev

Large String Object in SQLite Database

From Dev

Accessing Database Objects Using HTTP

From Dev

Query Repo Using a String

From Dev

Using states with query string

Related Related

  1. 1

    Using node-couchdb to query a database

  2. 2

    How to find documents in database using query string

  3. 3

    Navigate using router and pass an object as a query string

  4. 4

    node.js http 'get' request with query string parameters

  5. 5

    Query Cloudant (using the node.js module) database for a list of documents?

  6. 6

    Passing a query string to a request as an actual string using Node and Express

  7. 7

    How to store emoji string in Database using Laravel with Orm Query

  8. 8

    Replace string in all columns of a SQLite database using one simple query

  9. 9

    How do I save a string from database query using codeigniter

  10. 10

    HTTP DELETE using jQuery ajax with parameters in query string

  11. 11

    HTTP DELETE using jQuery ajax with parameters in query string

  12. 12

    Query string is appending when using $_SERVER['HTTP_REFERER']

  13. 13

    Node - Database query callback not firing

  14. 14

    using node.js how to find the returned value is string or [object]

  15. 15

    Struggling to translate jQuery JSONP query to a Node.JS query using http.request

  16. 16

    Using a function to query a database

  17. 17

    Convert Query String to Object

  18. 18

    Changing SQL string query to parametized query; syntax for calling database class file using C#

  19. 19

    How to execute a mongo query string on a node server using native node driver?

  20. 20

    Database query not returning full string

  21. 21

    Why am I apparently connecting to SQL Azure database using node,js but query is not executed?

  22. 22

    Negative selection http query string (IS NOT)

  23. 23

    HTTP Caching URLs with Query String

  24. 24

    bash - parse a http query string

  25. 25

    Not using database connection string

  26. 26

    Large String Object in SQLite Database

  27. 27

    Accessing Database Objects Using HTTP

  28. 28

    Query Repo Using a String

  29. 29

    Using states with query string

HotTag

Archive