gpt4 book ai didi

node.js - tunnel-ssh 连接后抛出错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:04 27 4
gpt4 key购买 nike

我正在尝试使用tunnel-ssh包连接到mongodb(mongodb包)。它已连接,我可以记录数据库,但它立即抛出错误并断开连接。

buffer.js:705
throw new ERR_INVALID_ARG_TYPE(
^

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type undefined
at Function.byteLength (buffer.js:705:11)
at SSH2Stream.directTcpip (D:\WORK\node_modules\ssh2-streams\lib\ssh.js:1128:23)
at openChannel (D:\WORK\node_modules\ssh2\lib\client.js:1142:21)
at Client.forwardOut (D:\WORK\node_modules\ssh2\lib\client.js:994:10)
at Client.<anonymous> (D:\WORK\node_modules\tunnel-ssh\index.js:16:23)
at Client.emit (events.js:223:5)
at SSH2Stream.<anonymous> (D:\WORK\node_modules\ssh2\lib\client.js:601:10)
at Object.onceWrapper (events.js:312:28)
at SSH2Stream.emit (events.js:223:5)
at parsePacket (D:\WORK\node_modules\ssh2-streams\lib\ssh.js:3911:10) {
code: 'ERR_INVALID_ARG_TYPE'
}

这是我的代码。

const tunnel = require("tunnel-ssh");
const config = require("config");
const MongoClient = require("mongodb").MongoClient;

const connection = new Promise((resolve, _) => {
// eslint-disable-next-line
tunnel(config.get("server"), async (err, server) => {
server.on("connection", console.log.bind(console, "server error"));
const client = await MongoClient.connect(config.get("mongodb").url, {
useUnifiedTopology: true,
useNewUrlParser: true
});
client.on("error", console.error.bind(console, "mongodb error"));
resolve({ client });
});
});

async function runQuery() {
const { client} = await connection;
console.log(client);
}

runQuery();

配置没有问题。事实上,runQuery 函数中的日志记录可以工作,但会立即抛出该错误。

最佳答案

我没有使用您提到的 tunnel-ssh 软件包,但我浏览了文档,发现您使用了错误的方法。我只是复制了tunnel-ssh 文档中给出的配置,它就开始为我工作了。粘贴下面的完整代码

const tunnel = require("tunnel-ssh");
const MongoClient = require("mongodb").MongoClient;

const connection = new Promise((resolve, _) => {
// eslint-disable-next-line
tunnel(
{
username: "root",
Password: "secret",
host: "127.0.0.1",
port: 22,
dstHost: "127.0.0.1",
dstPort: 27017,
localHost: "127.0.0.1",
localPort: 27000
},
async (err, server) => {
server.on("connection", console.log.bind(console, "server error"));
const client = await MongoClient.connect(
"mongodb://localhost:27017/user",
{
useUnifiedTopology: true,
useNewUrlParser: true
}
);
client.on("error", console.error.bind(console, "mongodb error"));
resolve({ client });
server.close();
}
);
});

async function runQuery() {
const { client } = await connection;
console.log("Connection Successful");
}

runQuery();

你出错的部分是将字符串传递给隧道包。它需要配置对象而不是字符串。

关于node.js - tunnel-ssh 连接后抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294270/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com