应用没有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 UNDEFINED
答案 0 :(得分:1)
您正在混合承诺代码和普通同步代码。这不是一个好主意,因为Promises可能无法立即完成。注意在示例中他们如何在REPL shell上逐个执行步骤,并且首先返回一个挂起的promise,然后是已解析的值。
由于代码中的步骤都完全取决于之前的步骤,我建议将所有内容放在连续的子句中。
db.createDatabase('mydb').then(...).then(() => db.useDatabase('mydb')).then(...)
等等。这将确保在执行依赖项后执行相关代码。