Parse json from url into mongodb - json

I'm trying to parse, some json data from an url and save it into my mongoDB model. however i can't seem to parse the JSON correctly from the body. How can i achieve this?
Code
router.get('/news', function(req, res){
request({
method: "GET",
url: "URL",
json: true
}, function(err, response, body) {
console.log(err);
res.json(body);
var info = JSON.parse(body);
console.log(info.articles);
})
});
Snippet of code from the api
{
"articles": [
{
"title": "this is the title",
"created": "12-09-2015",
"author": "John Doe",
"image": "http://url.com/test.jpg",
"body": "this is the body"
},
{
"title": "this is the title",
"created": "12-09-2015",
"author": "John Doe",
"image": "http://url.com/test.jpg",
"body": "this is the body"
}
]
}
News model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var newsSchema = new Schema({
title: String,
created: String,
author: String,
image: String,
bodyfull: String
});
module.exports = mongoose.model('news', newsSchema);

I'm not sure I understand exactly what's going on in your example there.
But if you want to retrieve information from a request body in node, you can from a POST request
so
var News = require('newsModel'); //wherever it's located
Router.post('/news', function(req,res){
var infoToParse = req.body;
var info = JSON.parse(infoToParse);
var newsItem = new News(info);
newsItem.save(function(err){
if(err) return handleError(err)
});
}
not sure if that helps at all? I'm no node guru but that's the kind of thing I do

Related

I want to get Affinity Interest from Google analytics api v4

I put the tracking code of google analytics in my web Application, now i want to get the Interests of the user from Google analytics API, I am using Nodejs, here is my request's code and the JSON response I get.
const dimensions_rows = [{
name: 'ga:interestAffinityCategory'
}, ];
const date_filters = [{
startDate: '7daysAgo',
endDate: 'today',
}];
const req = {
reportRequests: [{
viewId: viewId,
dateRanges: date_filters,
dimensions: dimensions_rows,
}],
};
analytics.reports.batchGet({
auth: oauth2Client,
requestBody: req
},
function(err, response) {
if (err) {
console.log('Failed to get Report');
console.log(err);
return;
}
// response.data is where the good stuff is located
console.log('Success - got something back from the Googlez');
console.log("responseee: ", JSON.stringify(response.data));
}
);
//JSON response
{
"reports": [{
"columnHeader": {
"dimensions": ["ga:interestAffinityCategory"],
"metricHeader": {
"metricHeaderEntries": [{
"name": "ga:visits",
"type": "INTEGER"
}]
}
},
"data": {
"totals": [{
"values": ["0"]
}]
}
}]
}

Postman raw data works but form-data not works on POST request in node

I'm facing some problem when using postman. When I'll try to send raw data in JSON(application/json) format, it get success.
Postman sending post request and succeded
But when I'll try to send form data it returns some error.
{
"error": {
"errors": {
"name": {
"message": "Path `name` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `{PATH}` is required.",
"type": "required",
"path": "name"
},
"kind": "required",
"path": "name",
"$isValidatorError": true
},
"price": {
"message": "Path `price` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `{PATH}` is required.",
"type": "required",
"path": "price"
},
"kind": "required",
"path": "price",
"$isValidatorError": true
}
},
"_message": "Product validation failed",
"message": "Product validation failed: name: Path `name` is required., price: Path `price` is required.",
"name": "ValidationError"
}
}
Postman errors
And here is my project code snippets
product.js
import express from 'express';
import mongoose from 'mongoose';
import Product from '../models/product.model';
router.post('/', (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product.save().then(result => {
console.log(result);
res.status(201).json({
message: 'Created product successfully',
createdProduct: {
name: result.name,
price: result.price,
_id: result._id,
request: {
type: 'GET',
url: `http://localhost:3000/products/${result._id}`
}
}
});
}).catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
product.model.js
import mongoose from 'mongoose';
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: {type: String, required: true},
price: {type: Number, required: true}
});
module.exports = mongoose.model('Product', productSchema);
you missing
app.use(bodyParser.urlencoded({ extended: true }));
then try in x-www-form-urlencoded
Use multer middleware:
const upload = require('multer')();
route.post('/', upload.any(), (req, res) => {
// your code...
});
Any data sent using postman's formdata is considered as multipart/formdata. You have to use multer or other similar library in order to parse formdata.
For x-www-form-urlencoded and raw data from postman does not require multer for parsing. You can parse these data either using body-parser or express built-in middlewares express.json() and express.urlencoded({ extended: false, })
Since multer returns a middleware, you can only access req.body inside multer(i.e fileFilter) or in the middleware that comes after multer BUT NOT BEFORE if you have not used multer as global middleware(which is bad idea).
for code snippets: https://stackoverflow.com/a/68588435/10146901
You need to use body-parser.
npm install body-parser --save
Then just add in your code
var bodyParser = require('body-parser')
app.use(bodyParser.json())
Details can be found https://www.npmjs.com/package/body-parser
Besides using body-parser, it will still return empty req.body that will lead to errors because you have validation in place. The reason it is returning empty req.body when sending POST request using form-data tru Postman is because body-parser can't handle multipart/form-data. You need a package that can handle multipart/form-data like multer. See https://www.npmjs.com/package/multer. Try using that package.
Use multer for get form data
inside upload function you can get req.body from form data
https://www.npmjs.com/package/multer

send header with put request node.js

var request=require('request');
var values = [
{
"id": "24",
"kind": "nature",
"data": {}
}
]
request.put("http://localhost:5000/api/article/",values,function (err,data,res) {
res=JSON.parse(res)
console.log(res)
})
I think it's kinda obvious what I'm trying to do here. Can someone please tell me what I'm doing wrong?? If I'm verry far of can someone set me on the right track?
var request = require('request');
var values = [
{
"id": "24",
"kind": "nature",
"data": {}
}
];
request({
method: 'PUT',
url: 'http://localhost:5000/api/article/',
body: values,
json: true,
headers: {
'User-Agent': 'request'
}
}, (err, res, body) => {
// ...
});
See the docs: https://www.npmjs.com/package/request#custom-http-headers

Parse JSON in NodeJS

I am trying to fetch a JSON from a remote API using a simple NodeJS HTTP server (it uses request to make HTTP calls). When I run the HTTP response through some JSON validator, it says it's a valid JSON, but I'm not able to manage accessing keys or even keys which are arrays etc.
JSON looks like:
{
"submissions": [{
"token": "db7fa11f970f376cc17c5f8d1760ab98",
"created_at": "2017-03-02T13:01:35Z",
"saved_at": "2017-03-02T12:50:35Z",
"changed_at": "2017-03-02T12:50:35Z",
"email": "",
"data": {
"CompName": "",
"Name": "TestFirma01",
"MA_Name": "Robert Dotzlaff",
"CASFunction": "TestFunktion01",
"CreateDate": "02.03.2017",
"Street1": "TestStrasse",
"Zip1": "12345",
"Town1": "Berlin",
"PhoneFieldStr4": "07225919241",
"MailFieldStr1": "tes#mpl.de",
"Category1": [
"CRM"
],
"Category2": [
"mpl Finance"
],
"gwBranch": [
"B2B",
"B2C"
],
"ITDANZAHLMA": "<25",
"MPLUSERPOT": "<5",
"TurnOver": "<50.000",
"gwBranch_Product": "Maschinen",
"gwBranch_Solution": "Keine",
"Konkurenz": "Nein",
"MPLEINFUEHRUNG1": null,
"MPLEINFUEHRUNG2": [
"> 12 Monate"
],
"MPLEINFUEHRUNG3": "02.03.2017",
"MPLINFRASTRUKTUR1": [
"ERP"
],
"MPLINFRASTRUKTUR2": [
"Lotus"
],
"MPLINFRASTRUKTUR3": [
"RDP-Anbindung"
],
"MPLINTTHEMA1": [
"Projektmanagement",
"Vertrieb"
],
"MPLINTTHEMA2": [
"Auswertungen",
"Zeiterfassung"
],
"MPLINTTHEMA3": [
"Sonstiges"
],
"MPLSONSTIGEINFOS": "Es muss schnell sein",
"MPLKONKPRODUKT": "",
"ANSPR_TEAM": "Robert D",
"ANSPR_Entscheider": "Ptrick R",
"MPLENTSCHEIDUNG": "02.03.2017",
"ITDKLASSIFIZIERUNG": [
"sehr gut"
],
"NEXT_ACTION": [
"Testzugang"
]
},
"attachments": []
}]
}
NodeJS script as follows:
'use strict'
const Hapi = require('hapi');
const Express = require('express');
const Request = require('request');
const Vision = require('vision');
const Handlebars = require('handlebars');
const _ = require('lodash');
const LodashFilter = require('lodash.filter');
const LodashTake = require('lodash.take');
const JSONStream = require('JSONStream');
const jQuery = require('jsdom');
const server = new Hapi.Server();
server.connection({
host: '127.0.0.1',
port: 3000,
routes: {
cors: {
origin: ['*'],
additionalHeaders: ['X-API-KEY']
},
}
});
server.register(Vision, (err) => {
server.views({
engines: {
html: Handlebars
},
relativeTo: __dirname,
path: './views',
});
});
server.start((err) => {
if (err) {
throw err;
}
getJSON();
console.log(`Server running at: ${server.info.uri}`);
});
function getJSON() {
// URL and APIKEY ommitted for security reasons
var as_host = '';
var as_apiKey = ''
var as_endpoint = '/api/public/v1/projects/';
var as_projectId = '736';
var as_endpointExt = '/submissions';
var as_url = as_host + as_endpoint + as_projectId + as_endpointExt;
var options = {
url: as_url,
headers: {
'X-API-KEY': as_apiKey,
},
json: true
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var jsonString1 = JSON.stringify(body);
var jsonObject = JSON.parse(jsonString1);
console.log("BODY2: " + jsonObject);
}
}
Request(options, callback);
}
The console.log("BODY2: " + jsonObject); outputs BODY2: [object Object] which is not what I want. When I remove json: true from the options variable for request, it outputs the JSON (or at least what looks like one), but I am still unable to access key / value pairs in the JSON. I need to access especially the data part of the JSON which contains the relevant data sets that need to be handed over to a second remote REST API (accepts only a special format, which is why I may not simply hand the retrieved JSON over to the other API). I have already tried several solutions and read a lot of posts on here, none of them seems to work for me (JSON.parse(), JSONStream.parse(), _.get() etc). Any help is much appreciated!

how to add json to backbone,js collection using fetch

I am trying to get backbone.js to load json.
The json loads but i am not sure how to get the items into my collection.
Or maybe that happens automatically and i just can't trace out. scope issue?
//js code
//model
var Client = Backbone.Model.extend({
defaults: {
name: 'nike',
img: "http://www.rcolepeterson.com/cole.jpg"
},
});
//collection
var ClientCollection = Backbone.Collection.extend({
defaults: {
model: Client
},
model: Client,
url: 'json/client.json'
});
//view
var theView = Backbone.View.extend({
initialize: function () {
this.collection = new ClientCollection();
this.collection.bind("reset", this.render, this);
this.collection.bind("change", this.render, this);
this.collection.fetch();
},
render: function () {
alert("test" + this.collection.toJSON());
}
});
var myView = new theView();
//json
{
"items": [
{
"name": "WTBS",
"img": "no image"
},
{
"name": "XYC",
"img": "no image"
}
]
}
Your json is not in the correct format, you can fix the json or add a hint to backbone in the parse method:
var ClientCollection = Backbone.Collection.extend({
defaults: {
model: Client
},
model: Client,
url: 'json/client.json',
parse: function(response){
return response.items;
}
});
Or fix your JSON:
[
{
"name": "WTBS",
"img": "no image"
},
{
"name": "XYC",
"img": "no image"
}
]
If you use rest api, try turn off these parameters:
Backbone.emulateHTTP
Backbone.emulateJSON