1

在没有 Foxx 的情况下应用教程:

https://www.arangodb.com/tutorials/tutorial-node-js/

Node.js 8.11.1 (x64)

arangoDB 3.3.7-1_win64

arangojs@6.2.3

文档已保存:meta._rev未定义

节点在本地服务器上运行良好

测试 app.js 代码:

const http = require('http');

const hostname = '127.0.0.1';
const port = 8529;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Database = require('arangojs').Database;
db = new Database('http://127.0.0.1:8529');
db.createDatabase('mydb').then(
  () => console.log('Database created'),
  err => console.error('Failed to create database:', err)
);
db.useDatabase('mydb');
collection = db.collection('firstCollection');
collection.create().then(
    () => console.log('Collection created'),
    err => console.error('Failed to create collection:', err)
);

doc = {
    _key: 'firstDocument',
    a: 'foo',
    b: 'bar',
    c: Date()
};
collection.save(doc).then(
    meta => console.log('Document saved:', meta._rev),
    err => console.error('Failed to save document:', err)
);

终端 :

Microsoft Windows [Version 10.0.16299.371]
(c) 2017 Microsoft Corporation. All rights reserved.

H:\TEST>node app.js
Server running at http://127.0.0.1:8529/
Database created
Collection created
Document saved: undefined

更多代码:

const http = require('http');

const hostname = '127.0.0.1';
const port = 8529;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Database = require('arangojs').Database;
db = new Database('http://127.0.0.1:8529');
db.createDatabase('mydb').then(
  () => console.log('Database created'),
  err => console.error('Failed to create database:', err)
);
db.useDatabase('mydb');
collection = db.collection('firstCollection');
collection.create().then(
    () => console.log('Collection created'),
    err => console.error('Failed to create collection:', err)
);

// until now everything is running as it should

doc = {
    _key: 'firstDocument',
    a: 'foo',
    b: 'bar',
    c: Date()
};
collection.save(doc).then(
    meta => console.log('Document saved:', meta._rev),
    err => console.error('Failed to save document:', err)
);
collection.update('firstDocument', {d: 'qux'}).then(
   meta => console.log('Document updated:', meta._rev),
   err => console.error('Failed to update document:', err)
);
collection.document('firstDocument').then(
    doc => console.log('Document:', JSON.stringify(doc, null, 2)),
    err => console.error('Failed to fetch document:', err)
);

终端 :

Microsoft Windows [Version 10.0.16299.371]
(c) 2017 Microsoft Corporation. All rights reserved.

H:\TEST>node app.js
Server running at http://127.0.0.1:8529/
Database created
Collection created
Document saved: undefined
Document updated: undefined
Document: "Hello World\n"

显然 meta._rev 是未定义的

4

1 回答 1

1

You're mixing promise code and normal synchronous code. That's not a good idea because Promises may not complete immediately. Notice how in the example they're executing steps one-by-one on the REPL shell, and it returns a pending promise at first, then the resolved value.

Since the steps in the code are all dependent on previous steps completing, I'd suggest putting everything in consecutive then clauses.

db.createDatabase('mydb').then(...).then(() => db.useDatabase('mydb')).then(...)

and so on. This will ensure dependent code gets executed after the dependency gets executed.

于 2018-04-20T16:57:50.320 回答