Please help me. I need variable in my search use post:
app.post('/find', function(req, res) {
var id_school = req.body.std_id;
console.log('show '+ id_sekolah);
db.collection('ak_test_score', function(err, collection) {
collection.find({'std_id':id_school}).toArray(function(err, level) {
var a = level.std_id;
var b = level.school_name;
});
});
res.redirect('/test_score'); // send to my page to get
};
var test = a; // not defined variable a not have
app.get('/test_score', function(req, res) {
var id_school = test;
console.log('show '+ id_sekolah);
db.collection('ak_test_score', function(err, collection) {
collection.find({'std_id':id_school}).toArray(function(err, level) {
res.send(level)
});
});
};
I am using this for a website search using post.
app.post('/find', function(req, res) {
var id_school = req.body.std_id;
console.log('show '+ id_sekolah);
db.collection('ak_test_score', function(err, collection) {
collection.find({'std_id':id_school}).toArray(function(err, level) {
var a = level.std_id;
app.set('data',a);
var b = level.school_name;
});
});
res.redirect('/test_score'); ///// send to my page to get
};
app.get('/test_score', function(req, res) {
var id_school = app.get('data');
console.log('show '+ id_sekolah);
db.collection('ak_test_score', function(err, collection) {
collection.find({'std_id':id_school}).toArray(function(err, level) {
res.send(level)
});
});
};
Related
I using postgres sql, nodejs, express
app.get("/topic/create", function(req, res) {
var sql = "SELECT id, title FROM topic";
client.query(sql, function(err, res2) {
console.log(res2.rows);
//res.render("create", { topics: res2.rows });
});
});
this code is my router code
but when i enter that url it was error
error: invalid input syntax for integer: "create"
name: 'error',
length: 110,
severity: 'ERROR',
code: '22P02', line: '62',routine: 'pg_atoi'
i dont know reason because sql was working on other url only that app.get code doesn't working
//라우팅 작업
app.get("/", function(req, res) {
res.redirect("/topic");
});
app.get("/topic", function(req, res) {
var sql = "SELECT id, title FROM topic";
client.query(sql, function(err, res2) {
if (err) {
console.log(err);
} else {
res.render("view", { topics: res2.rows });
}
});
});
app.get("/topic/:id", function(req, res) {
var id = req.params.id;
var sql1 = "SELECT id, title FROM topic";
var sql2 = "SELECT * FROM topic where id=$1";
client.query(sql2, [id], function(err, res2) {
if (err) {
console.log(err);
} else {
client.query(sql1, function(err, res3) {
if (err) {
console.log(err);
res.status(500).send("Internal Server Error");
} else {
var list = [];
var result = res3.rows;
for (var i = 0; i < result.length; i++) {
list.push(res3.rows[i]);
}
res.render("view", { details: res2.rows, topics: list });
}
});
}
});
});
this is my router code it was same. this code is good working
i dont know why only that url make error
app.get("/topic/create", function(req, res) {
var sql = "SELECT id, title FROM topic";
client.query(sql, function(err, res2) {
console.log(res2.rows);
//res.render("create", { topics: res2.rows });
});
});
You have to put this router first then below one,
app.get("/topic/:id", function(req, res) {
var id = req.params.id;
var sql1 = "SELECT id, title FROM topic";
var sql2 = "SELECT * FROM topic where id=$1";
client.query(sql2, [id], function(err, res2) {
if (err) {
console.log(err);
} else {
client.query(sql1, function(err, res3) {
if (err) {
console.log(err);
res.status(500).send("Internal Server Error");
} else {
var list = [];
var result = res3.rows;
for (var i = 0; i < result.length; i++) {
list.push(res3.rows[i]);
}
res.render("view", { details: res2.rows, topics: list });
}
});
}
});
});
this will work, Because express take and match with first one. It get failed, That's why you got this error.
as #tadman mentioned, you need to define app.get("/topic/create function before the app.get("/topic/:id" function. otherwise, express thinks that you are executing app.get("/topic/:id" and the id is create.
Hope this helps.
I wrote the unit tests:
var app = require('../server');
var chai = require('chai');
var supertest = require("supertest")(app);
var GoogleUrl = require('google-url');
var config = require('../config');
var expect = chai.expect;
describe('Urls Tests', function () {
var url = {
author : 'Alexey',
description : 'grrggr',
full_url : 'https://github.com',
date : '30-06-2017',
time : '18:21:27',
count_click : 0,
list_tags : [
'Sport',
'Football'
]
};
var token;
beforeEach(function (done) {
agent
.post('http://localhost:8000/auth/login')
.send({email: 'Keane95#yandex.ru', password: '123456'})
.end(function (err, res) {
if (err) {
return done(err);
}
expect(res.body.userData).to.have.property('token');
token = res.body.userData.token;
done();
});
});
it('should create a url', function(done) {
var googleUrl = new GoogleUrl({
'key': config.get('google_key')
});
googleUrl.shorten(url.full_url, function (err, shortUrl) {
url.short_url = shortUrl;
supertest
.post('/urls/create')
.send(url)
.expect(401)
.end(function (err, res) {
if (err) return done(err);
expect(res.body.author).to.equal('Alexey');
url = res.body;
done();
});
});
});
it('should modify a url by id', function(done) {
url.description = 'Good description';
url.list_tags.push('Liverpool');
supertest
.put('/urls/' + url._id)
.send(url)
.expect(401)
.end(function(err, res) {
if (err) return done(err);
expect(res.body.description).to.equal('Good description');
expect(res.body.list_tags[2]).to.equal('Liverpool');
done();
});
});
it('should modify a count of clicks', function(done) {
url.count_click++;
supertest
.put('/urls/' + url._id)
.send(url)
.expect(401)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to.equal('Count of the click is updated');
done();
});
});
});
I run to execute the unit tests and get the errors:
I read the articles by unit tests.
First article: http://developmentnow.com/2015/02/05/make-your-node-js-api-bulletproof-how-to-test-with-mocha-chai-and-supertest/
Second article: https://www.codementor.io/olatundegaruba/integration-testing-supertest-mocha-chai-6zbh6sefz
I don't understand why I get these errors. Please, help me. I think that I made little error, but since I cannot fint it.
UPDATED
I added route:
var express = require('express');
var GoogleUrl = require('google-url');
var _ = require('lodash');
var token = require('../middlewares/token');
var Url = require('../models/url');
var config = require('../config');
var router = express();
router.post('/create', token.required, createShortUrl);
router.put('/count/:id', token.required, updateCountClick);
router.put('/:id', token.required, updateUrlById);
module.exports = router;
function createShortUrl(req, res) {
_.trim(req.body.list_tags);
var tags = _.split(req.body.list_tags, ',');
tags.splice(tags.length - 1, 1);
var date = returnDate();
var time = returnTime();
var googleUrl = new GoogleUrl({
'key': config.get('google_key')
});
googleUrl.shorten(req.body.full_url, function (err, shortUrl) {
if (err) {
res.status(500).json(err);
}
var url = new Url({
'author': req.payload.username,
'description': req.body.description,
'full_url': req.body.full_url,
'short_url': shortUrl,
'list_tags': tags,
'date': date,
'time': time
});
url.save(function (err, url) {
if (err) {
return res.status(500).json(err);
} else {
return res.status(200).json(url);
}
});
});
}
function updateCountClick(req, res) {
var count_click = req.body.count_click + 1;
Url.findOneAndUpdate({_id: req.params.id}, {$set: {count_click: count_click}}, {new: true}, function (err, url) {
if (err) {
return res.status(500).json(err);
}
if (url) {
return res.status(200).json('Count of the click is updated');
}
});
}
function updateUrlById(req, res) {
_.trim(req.body.list_tags);
var tags = _.split(req.body.list_tags, ',');
tags.splice(tags.length - 1, 1);
Url.findOneAndUpdate({_id: req.params.id}, {$set: {description: req.body.description, list_tags: tags}}, {new: true}, function (err, url) {
if (err) {
res.status(500).json(err);
}
if (url) {
res.status(200).json(url);
}
});
}
UPDATED 2
Authoziration was added:
var token;
beforeEach(function (done) {
agent
.post('http://localhost:8000/auth/login')
.send({email: 'Keane95#yandex.ru', password: '123456'})
.end(function (err, res) {
if (err) {
return done(err);
}
expect(res.body.userData).to.have.property('token');
token = res.body.userData.token;
done();
});
});
Also I updated code my unit-tests.
I can't see where in your code you send 401 and Url. So it seems that your test requests are getting rejected by token.required middleware with 401 status code (which means "unauthorized").
.send(url)
.expect(401) // why do you expect 401? You never send it inside your logic
So basically your test never hit actual code.
First of all, you do need to fake authorization to make token.required middleware happy.
Then expect 200 result
.send(url)
.expect(200) // normal execution flow of createShortUrl results in 200
.end(/* rest of your test logic */)
I am trying to download more that 100 files at the same time. But when I execute the downloading function my macbook freezes(unable to execute new tasks) in windows also no download(but doesn't freeze) and no download progress in both case(idle network).
Here is my download module:
var express = require('express');
var router = express.Router();
var fs = require('fs');
var youtubedl = require('youtube-dl');
var links = require('../models/Links');
router.get('/', function (req, res, next) {
links.find({dlStatus: false}, function (err, docs) {
if (err) {
console.log(err);
res.end();
} else if (!docs) {
console.log('No incomplete downloads!');
res.end();
} else {
for (var i = 0; i < docs.length; i++) {
//todo scraping
var video = youtubedl(docs[i].url, [], {cwd: __dirname});
// Will be called when the download starts.
video.on('info', function (info) {
console.log('Download started');
console.log(info);
});
video.pipe(fs.createWriteStream('./downloads/' + docs[i].id + '-' + i + '.mp4'));
video.on('complete', function complete(info) {
links.findOneAndUpdate({url: info.webpage_url}, {dlStatus: true}, function (err, doc) {
if (err)console.log(err);
else console.log('Download completed!')
});
});
}
}
});
});
module.exports = router;
Now can anyone please help me here? I am using this module for downloading files.
The solution is using async in this case.
Try it this way....with async.each()
var express = require('express');
var router = express.Router();
var fs = require('fs');
var youtubedl = require('youtube-dl');
var links = require('../models/Links');
var async = require('async')
router.get('/', function (req, res, next) {
links.find({dlStatus: false}, function (err, docs) {
if (err) {
console.log(err);
res.end();
} else if (!docs) {
console.log('No incomplete downloads!');
res.end();
} else {
async.each(docs,function(doc,cb){
var video = youtubedl(doc.url, [], {cwd: __dirname});
// Will be called when the download starts.
video.on('info', function (info) {
console.log('Download started');
console.log(info);
});
video.pipe(fs.createWriteStream('./downloads/' + docs.id + '-' + i + '.mp4'));
video.on('complete', function complete(info) {
links.findOneAndUpdate({url: info.webpage_url}, {dlStatus: true}, function (err, doc) {
if (err){
console.log(err);
cb(err);
}
else {
console.log('Download completed!');
cb()
}
});
});
},function(err){
if(err)
return console.log(err);
console.log("Every thing is done,Here!!");
})
}
});
});
module.exports = router;
And you can process every thing in batch too using async.eachLimits().
I have set up node and mongodb and have imported some yelp data into mongo. When I query using the mongo shell, I can see there are documents and everything is fine. However I'm unable to pass them along by adding them to an array and returning that array. When I hit up localhost:3000/api/reviews, I get a blank page. My console does log everything though so the node driver for mongo is working in getting the documents. Any ideas? I feel like it has something to do with the asynchronous nature of node.
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://localhost:27017/YelpDB';
var getReviews = function(db, callback) {
var cursor = db.collection('reviews').find( );
//JSONArray jsonarray = new JSONArray();
var data = [];
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
var jsonDoc = JSON.stringify(doc);
console.log(typeof jsonDoc);
data.push(jsonDoc);
} else {
callback();
}
});
return data;
};
router.get('/reviews/', function(req, res, next) {
//res.send('respond with a resource');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var data = getReviews(db, function() {
db.close();
});
res.json({"reviews": data});
});
});
Please try this one, you should return the data at the end of cursor.each in the callback function.
var getReviews = function(db, callback) {
var cursor = db.collection('reviews').find( );
var data = [];
cursor.each(function(err, doc) {
if (err)
callback(err);
if (doc) {
var jsonDoc = JSON.stringify(doc);
console.log(typeof jsonDoc);
data.push(jsonDoc);
} else {
// at the end of cursor, return the data through callback
callback(null, data);
}
});
};
router.get('/reviews/', function(req, res, next) {
//res.send('respond with a resource');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
getReviews(db, function(err, data) {
if (err)
throw err;
// send the data in callback function
res.json({"reviews": data});
db.close();
});
});
});
Hi there I'm starting to Develop Restful API using Restify but I'm having problems developing [PUT].
My idea is to get the params and change it using the new params. But this code does not seem to work. Please help me out :D
server.put('/user/:_id', function (req, res, next) {
var user = {};
user._id = req.params._id;
console.log('_id === '+user);
var changes = req.params;
delete changes._id;
// delete changes._id;
for(var x in changes) {
user[x] = changes[x];
console.log('user[x] ='+user[x]);
}
db.Student.update(req.params._id, user,{multi:true,upsert:true}, function(err,data){
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
res.end(JSON.stringify(user));
return next();
});
});
here is the putclient
var restify = require('restify');
var server = require('./app');
var client = restify.createJsonClient({
url: 'http://localhost:3000'
});
var testUser = {
$set:
{
"name" : "procopio magalpok",
"yearLevel" : "IX"
}
};
testUser._id = "561de16f885fb2f40d23ece",
client.put('/user/'+testUser._id, testUser , function (err, req, res, status) {
if (err) {
console.log("An error ocurred >>>>>>");
console.log(err);
} else {
console.log('id : '+testUser._id);
console.log('User updated >>>>>>>');
console.log(status);
}
});