-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
53 lines (46 loc) · 1.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('./').fs;
const mongodb = require('./').mongodb;
const http = require('http');
async function testReadFile() {
// if you has muilti await calls in one function
// and use the same error variable name just like err
// pls use `var` for declaration, because when using
// using `let` that node will throw re-define exception
var [err, data] = await fs.writeFile('foo.txt', new Buffer('hello world\n'), 'utf8');
if (err) return console.log(err);
var [err, data] = await fs.readFile('foo.txt', 'utf8');
if (err) return console.log(err);
// if you want to just return this
// and throw error handling for other invokers
// just return err and data
// return [err, data];
var [err, db] = await mongodb.db('mongodb://localhost:27017/test');
if (err) throw err;
let main = db.collection('main');
var [err, data] = await main.find({}).skip(10).limit(3).toArray();
// console.log(err, rep)
var [err, rep] =await main.insert({foo: 'bar'});
// console.log(err, rep);
var [err, rep] = await main.update({foo: 'bar'}, {$set: {foo: 'fool'}}, {multi: true});
// console.log(err, rep);
var [err, rep] = await main.remove({foo: 'bar'});
var [err, rep] = await main.findOne({foo: 'fool'});
// console.log(err, rep);
// if (err) return console.log(err)
// console.log('***')
// console.log(rep)
// console.log('***')
// let main = db.collection('main');
// var [err, rep] = main.insert({foo: 'bar'});
// if (err) return console.log(err);
// console.log(rep);
// main.insert({foo: 'bar'}, (err, rep) => {
// if (err) return console.log(err);
// console.log(rep)
// })
}
testReadFile()
.then((result) => {
// console.log(result);// [undefined, 'hello world\n']
})
.catch(err => console.log(err));