1

Applying tutorial without Foxx :

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

Node.js 8.11.1 (x64)

arangoDB 3.3.7-1_win64

[email protected]

DOCUMENT SAVED : meta._rev UNDEFINED

node is running well in local server

Test app.js code :

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)
);

Terminal :

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

More code :

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)
);

Terminal :

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"

Apparently meta._rev is UNDEFINED

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.