提交 4712b67a 编写于 作者: D dingbo

docs: node

上级 50f75a53
...@@ -71,7 +71,7 @@ In this example, we use `exec` method to execute SQL. `exec` is designed for som ...@@ -71,7 +71,7 @@ In this example, we use `exec` method to execute SQL. `exec` is designed for som
<TabItem value="node" label="Node.js"> <TabItem value="node" label="Node.js">
```javascript ```javascript
{{#include docs/examples/node/insert.js}}
``` ```
</TabItem> </TabItem>
......
...@@ -209,5 +209,10 @@ Get all rows and print each row: ...@@ -209,5 +209,10 @@ Get all rows and print each row:
</TabItem> </TabItem>
<TabItem value="node" label="Node.js"> <TabItem value="node" label="Node.js">
```javascript
{{#include docs/examples/node/query.js}}
```
</TabItem> </TabItem>
</Tabs> </Tabs>
\ No newline at end of file
const { options, connect } = require("td2.0-rest-connector");
function checkError(result) {
if (result.getErrCode() !== undefined) {
console.log(result.getErrCode(), result.getErrStr());
process.exit(1);
}
}
async function test() {
options.url = process.env.TDENGINE_CLOUD_URL;
options.query = { token: process.env.TDENGINE_CLOUD_TOKEN };
let conn = connect(options);
let cursor = conn.cursor();
try {
let result = await cursor.query("DROP DATABASE IF EXISTS power");
checkError(result);
result = await cursor.query("CREATE DATABASE power");
checkError(result);
result = await cursor.query("CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)");
checkError(result);
result = await cursor.query("INSERT INTO power.d1001 USING power.meters TAGS(California.SanFrancisco, 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) power.d1002 USING power.meters TAGS(California.SanFrancisco, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)");
checkError(result);
console.log("AffectedRows:", result.getAffectRows())
} catch (err) {
console.log(err);
}
}
test();
const { options, connect } = require("td2.0-rest-connector");
function checkError(result) {
if (result.getErrCode() !== undefined) {
console.log(result.getErrCode(), result.getErrStr());
process.exit(1);
}
}
async function test() {
options.url = process.env.TDENGINE_CLOUD_URL;
options.query = { token: process.env.TDENGINE_CLOUD_TOKEN };
let conn = connect(options);
let cursor = conn.cursor();
try {
let result = await cursor.query("SELECT ts, current FROM power.meters LIMIT 2");
console.log(result.getMeta());
// [
// { columnName: 'ts', code: 9, size: 8, typeName: 'timestamp' },
// { columnName: 'current', code: 6, size: 4, typeName: 'float' }
// ]
console.log(result.getData());
// [ [ '2018-10-03T14:38:05Z', 10.3 ], [ '2018-10-03T14:38:15Z', 12.6 ] ]
} catch (err) {
console.log(err);
}
}
test();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册