[英]Unhandled Promise rejection warning: TypeError: First argument must be a string or Buffer
[英]Nodejs - rethinkdb Unhandled rejection ReqlDriverError: First argument to `run` must be an open connection
按照官方文档运行简单代码,但报错
const r = require('rethinkdb');
var connection = null;
r.connect( {host: 'localhost', port: 28015}, function(err, conn) {
if (err) throw err;
connection = conn;
})
// r.dbCreate("blog").run(connection, function(err, result) {
// if (err) throw err;
// console.log(result);
// });
r.db('test').tableCreate('authors').run(connection, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
})
未处理的拒绝 ReqlDriverError: run
的第一个参数必须是打开的连接。 在 ReqlDriverError.ReqlError [作为构造函数] (/Users/tejastank/source/electronjs/website-scanner/node_modules/rethinkdb/errors.js:23:13) 在新的 ReqlDriverError (/Users/tejastank/source/electronjs/website-scanner /node_modules/rethinkdb/errors.js:68:50) 在 HTMLButtonElement 的 Function.TermBase.run (/Users/tejastank/source/electronjs/website-scanner/node_modules/rethinkdb/ast.js:133:29)。 (file:///Users/tejastank/source/electronjs/website-scanner/index.js:59:41) (node:45504) [DEP0005] DeprecationWarning: Buffer() 由于安全性和可用性问题而被弃用。 请改用 Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from() 方法。
connection
在.connect()
回调中填充,不能保证在调用后完全可用。 您需要在回调中工作:
const r = require('rethinkdb');
r.connect( {host: 'localhost', port: 28015}, function(err, conn) {
if (err) throw err;
r.db('test').tableCreate('authors').run(conn, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
});
});
或通过使用承诺:
const r = require('rethinkdb');
r.connect({host: 'localhost', port: 28015})
.then(function(conn) {
return r.db('test').tableCreate('authors').run(conn);
})
.then(function(result) {
console.log(JSON.stringify(result, null, 2));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.