這個(gè)Javascript“要求”是什么?我試圖讓Javascript讀取/寫入PostgreSQL數(shù)據(jù)庫(kù)。我發(fā)現(xiàn)了這個(gè)工程項(xiàng)目在GitHub上。我能夠在節(jié)點(diǎn)中運(yùn)行以下示例代碼。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();});接下來(lái),我試圖讓它在網(wǎng)頁(yè)上運(yùn)行,但似乎什么也沒(méi)有發(fā)生。我查看了Javascript控制臺(tái),它只說(shuō)“RequireNotDefined”。那么這“要求”是什么呢?為什么它在節(jié)點(diǎn)中工作,而在網(wǎng)頁(yè)中卻不起作用?而且,在我讓它在節(jié)點(diǎn)上工作之前,我必須做npm install pg..那是怎么回事?我查看了目錄,沒(méi)有找到PG文件。它把它放在哪里,Javascript是怎么找到它的?
這個(gè)Javascript“要求”是什么?
慕娘9325324
2019-07-01 15:01:32