Showing
2 changed files
with
0 additions
and
67 deletions
test.js
deleted
100644 → 0
1 | -// | ||
2 | -const joke = require('./jokes/jokes.json'); | ||
3 | -const MongoClient = require('mongodb').MongoClient; | ||
4 | - | ||
5 | - | ||
6 | -MongoClient.connect('mongodb://localhost:27017', function (err, client){ | ||
7 | - if (err) throw err; | ||
8 | - var db = client.db('jokeapi'); | ||
9 | - | ||
10 | - question = []; | ||
11 | - punchline = []; | ||
12 | - count = 0; | ||
13 | - json_max = 376; | ||
14 | - random = function getRandomArbitrary() { | ||
15 | - return Math.random() * (376 - 1) + 1; | ||
16 | - } | ||
17 | - for(var i=1; i< json_max+1; i++){ | ||
18 | - result = db.collection('jokes').findOne({id: i,type:"programming"}); | ||
19 | - | ||
20 | - if(result === null) | ||
21 | - return; | ||
22 | - else if(result) | ||
23 | - { | ||
24 | - ++count; | ||
25 | - question.push(result.setup); | ||
26 | - punchline.push(result.punchline); | ||
27 | - // var n = result.count(result); | ||
28 | - // var r = Math.floor(Math.random() * n); | ||
29 | - // var randomElement = result.find().limit(1).skip(r); | ||
30 | - // var question = randomElement.setup; | ||
31 | - // var joke = randomElement.punchline; | ||
32 | - // console.log(question); | ||
33 | - // console.log(joke); | ||
34 | - // console.log(question); | ||
35 | - // console.log(punchline); | ||
36 | - //console.log(question); | ||
37 | - } | ||
38 | - } | ||
39 | - client.close(); | ||
40 | - console.log(question); | ||
41 | -}); | ||
42 | - |
test2.js
deleted
100644 → 0
1 | -const MongoClient = require('mongodb').MongoClient; | ||
2 | - | ||
3 | - | ||
4 | -//Get Random Joke | ||
5 | -MongoClient.connect('mongodb://localhost:27017', function (err, client){ | ||
6 | - if (err) throw err; | ||
7 | - var db = client.db('jokeapi'); | ||
8 | - | ||
9 | - json_max = 376; | ||
10 | - function getRandomInt() { | ||
11 | - min = Math.ceil(1); | ||
12 | - max = Math.floor(376); | ||
13 | - return Math.floor(Math.random() * (max - min + 1)) + min; | ||
14 | - } | ||
15 | - random = getRandomInt(); | ||
16 | - console.log(random); | ||
17 | - result = db.collection('jokes').findOne({id: random}); | ||
18 | - | ||
19 | - user = result; | ||
20 | - user.then(function(total){ | ||
21 | - console.log(total.setup); | ||
22 | - console.log(total.setup); | ||
23 | - }) | ||
24 | - client.close(); | ||
25 | -}) |
-
Please register or login to post a comment