這個Javascript“要求”是什么?我試圖讓Javascript讀取/寫入PostgreSQL數據庫。我發現了這個工程項目在GitHub上。我能夠在節點中運行以下示例代碼。var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`var conString = "tcp://postgres:1234@localhost/postgres";var client = new pg.Client(conString);client.connect();//queries are queued and executed one after another once the connection becomes availableclient.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);//queries can be executed either via text/parameter values passed as individual arguments//or by passing an options object containing text, (optional) parameter values, and (optional) query nameclient.query({
name: 'insert beatle',
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
values: ['George', 70, new Date(1946, 02, 14)]});//subsequent queries with the same name will be executed without re-parsing the query plan by postgresclient.query({
name: 'insert beatle',
values: ['Paul', 63, new Date(1945, 04, 03)]});var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);//can stream row results back 1 at a timequery.on('row', function(row) {
console.log(row);
console.log("Beatle name: %s", row.name); //Beatle name: John
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints});//fired after last row is emittedquery.on('end', function() {
client.end();});接下來,我試圖讓它在網頁上運行,但似乎什么也沒有發生。我查看了Javascript控制臺,它只說“RequireNotDefined”。那么這“要求”是什么呢?為什么它在節點中工作,而在網頁中卻不起作用?而且,在我讓它在節點上工作之前,我必須做npm install pg..那是怎么回事?我查看了目錄,沒有找到PG文件。它把它放在哪里,Javascript是怎么找到它的?
這個Javascript“要求”是什么?
慕娘9325324
2019-07-01 15:01:32