node.js 中的 javascript 代码和 rethinkdb 作为后端有什么问题?
What is wrong with this javascript code in node.js and with rethinkdb as backend?
我的javascript代码如下:
var r = require('rethinkdb');
var soll1=null;
var soll=null;
r.connect( {host: 'localhost', port: 28015}, function(err, conn) {
r.db("discogs").table("releases").limit(4).run(conn, function(err, cursor) {
if (err) throw err;
cursor.toArray(function(err, result) {
if (err) throw err;
soll=result.country;
});
});
});
var http = require('http');
http.createServer(function (req, res) {res.write(soll);res.end();}).listen(3001, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3001/');
我在rethinkdb中的JSON如下。我想提取 "country" 值:
{
"release":{
"genres":{
"genre":"Electronic"
},
"identifiers":{
"identifier":[
{
"description":"A-Side",
"value":"MPO SK 032 A1 G PHRUPMASTERGENERAL T27 LONDON",
"type":"Matrix / Runout"
},
{
"description":"B-Side",
"value":"MPO SK 032 B1",
"type":"Matrix / Runout"
},
{
"description":"C-Side",
"value":"MPO SK 032 C1",
"type":"Matrix / Runout"
},
{
"description":"D-Side",
"value":"MPO SK 032 D1",
"type":"Matrix / Runout"
}
]
},
"status":"Accepted",
"videos":{
"video":[
{
"title":"The Persuader (Jesper Dahlbäck) - Östermalm",
"duration":290,
"description":"The Persuader (Jesper Dahlbäck) - Östermalm",
"src":"http://www.youtube.com/watch?v=AHuQWcylaU4",
"embed":true
},
{
"title":"The Persuader - Vasastaden",
"duration":380,
"description":"The Persuader - Vasastaden",
"src":"http://www.youtube.com/watch?v=5rA8CTKKEP4",
"embed":true
},
{
"title":"The Persuader-Stockholm-Sodermalm",
"duration":335,
"description":"The Persuader-Stockholm-Sodermalm",
"src":"http://www.youtube.com/watch?v=QVdDhOnoR8k",
"embed":true
},
{
"title":"The Persuader - Norrmalm",
"duration":289,
"description":"The Persuader - Norrmalm",
"src":"http://www.youtube.com/watch?v=hy47qgyJeG0",
"embed":true
}
]
},
"labels":{
"label":{
"catno":"SK032",
"name":"Svek"
}
},
"companies":{
"company":[
{
"id":271046,
"catno":"",
"name":"The Globe Studios",
"entity_type_name":"Recorded At",
"resource_url":"http://api.discogs.com/labels/271046",
"entity_type":23
},
{
"id":56025,
"catno":"",
"name":"MPO",
"entity_type_name":"Pressed By",
"resource_url":"http://api.discogs.com/labels/56025",
"entity_type":17
}
]
},
"styles":{
"style":"Deep House"
},
"formats":{
"format":{
"text":"",
"name":"Vinyl",
"qty":2,
"descriptions":{
"description":[
"12\"",
"33 ⅓ RPM"
]
}
}
},
"country":"Sweden",
"id":1,
"released":"1999-03-00",
"artists":{
"artist":{
"id":1,
"anv":"",
"name":"Persuader, The",
"role":"",
"tracks":"",
"join":""
}
},
"title":"Stockholm",
"master_id":5427,
"tracklist":{
"track":[
{
"position":"A",
"duration":"4:45",
"title":"Östermalm"
},
{
"position":"B1",
"duration":"6:11",
"title":"Vasastaden"
},
{
"position":"B2",
"duration":"2:49",
"title":"Kungsholmen"
},
{
"position":"C1",
"duration":"5:38",
"title":"Södermalm"
},
{
"position":"C2",
"duration":"4:52",
"title":"Norrmalm"
},
{
"position":"D",
"duration":"5:16",
"title":"Gamla Stan"
}
]
},
"data_quality":"Complete and Correct",
"extraartists":{
"artist":{
"id":239,
"anv":"",
"name":"Jesper Dahlbäck",
"role":"Music By [All Tracks By]",
"tracks":"",
"join":""
}
},
"notes":"The song titles are the names of Stockholm's districts."
}
}
当我尝试使用 node.js 运行 时,出现以下错误:
http.js:852
throw new TypeError('first argument must be a string or Buffer');
^
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.write (http.js:852:11)
at Server.<anonymous> (/home/dhiraj/http_server.js:19:44)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2109:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
at Socket.socket.ondata (http.js:1967:22)
at TCP.onread (net.js:528:27)
抱歉,如果我的问题看起来很麻烦。但是我不明白我哪里错了?
如错误消息所述,res.write
需要字符串或 Buffer 对象,因此您必须确保输出为字符串。
soll=result.country;
您在某些 callback 函数中设置了 soll
变量,与 res.write(soll);
语句相比,这些函数将在稍后的时间点执行。
所以为了调试目的你可以试试
res.write(soll?soll:'soll has null value');
我希望你会看到
soll has null value
这样至少你能明白你的情况出了什么问题:D
希望这对您有所帮助:D
当您尝试发送时,变量 soll 很可能仍然为 null。
放
console.log('soll:', soll);
就在 res.write...之前检查它。
我的javascript代码如下:
var r = require('rethinkdb');
var soll1=null;
var soll=null;
r.connect( {host: 'localhost', port: 28015}, function(err, conn) {
r.db("discogs").table("releases").limit(4).run(conn, function(err, cursor) {
if (err) throw err;
cursor.toArray(function(err, result) {
if (err) throw err;
soll=result.country;
});
});
});
var http = require('http');
http.createServer(function (req, res) {res.write(soll);res.end();}).listen(3001, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3001/');
我在rethinkdb中的JSON如下。我想提取 "country" 值:
{
"release":{
"genres":{
"genre":"Electronic"
},
"identifiers":{
"identifier":[
{
"description":"A-Side",
"value":"MPO SK 032 A1 G PHRUPMASTERGENERAL T27 LONDON",
"type":"Matrix / Runout"
},
{
"description":"B-Side",
"value":"MPO SK 032 B1",
"type":"Matrix / Runout"
},
{
"description":"C-Side",
"value":"MPO SK 032 C1",
"type":"Matrix / Runout"
},
{
"description":"D-Side",
"value":"MPO SK 032 D1",
"type":"Matrix / Runout"
}
]
},
"status":"Accepted",
"videos":{
"video":[
{
"title":"The Persuader (Jesper Dahlbäck) - Östermalm",
"duration":290,
"description":"The Persuader (Jesper Dahlbäck) - Östermalm",
"src":"http://www.youtube.com/watch?v=AHuQWcylaU4",
"embed":true
},
{
"title":"The Persuader - Vasastaden",
"duration":380,
"description":"The Persuader - Vasastaden",
"src":"http://www.youtube.com/watch?v=5rA8CTKKEP4",
"embed":true
},
{
"title":"The Persuader-Stockholm-Sodermalm",
"duration":335,
"description":"The Persuader-Stockholm-Sodermalm",
"src":"http://www.youtube.com/watch?v=QVdDhOnoR8k",
"embed":true
},
{
"title":"The Persuader - Norrmalm",
"duration":289,
"description":"The Persuader - Norrmalm",
"src":"http://www.youtube.com/watch?v=hy47qgyJeG0",
"embed":true
}
]
},
"labels":{
"label":{
"catno":"SK032",
"name":"Svek"
}
},
"companies":{
"company":[
{
"id":271046,
"catno":"",
"name":"The Globe Studios",
"entity_type_name":"Recorded At",
"resource_url":"http://api.discogs.com/labels/271046",
"entity_type":23
},
{
"id":56025,
"catno":"",
"name":"MPO",
"entity_type_name":"Pressed By",
"resource_url":"http://api.discogs.com/labels/56025",
"entity_type":17
}
]
},
"styles":{
"style":"Deep House"
},
"formats":{
"format":{
"text":"",
"name":"Vinyl",
"qty":2,
"descriptions":{
"description":[
"12\"",
"33 ⅓ RPM"
]
}
}
},
"country":"Sweden",
"id":1,
"released":"1999-03-00",
"artists":{
"artist":{
"id":1,
"anv":"",
"name":"Persuader, The",
"role":"",
"tracks":"",
"join":""
}
},
"title":"Stockholm",
"master_id":5427,
"tracklist":{
"track":[
{
"position":"A",
"duration":"4:45",
"title":"Östermalm"
},
{
"position":"B1",
"duration":"6:11",
"title":"Vasastaden"
},
{
"position":"B2",
"duration":"2:49",
"title":"Kungsholmen"
},
{
"position":"C1",
"duration":"5:38",
"title":"Södermalm"
},
{
"position":"C2",
"duration":"4:52",
"title":"Norrmalm"
},
{
"position":"D",
"duration":"5:16",
"title":"Gamla Stan"
}
]
},
"data_quality":"Complete and Correct",
"extraartists":{
"artist":{
"id":239,
"anv":"",
"name":"Jesper Dahlbäck",
"role":"Music By [All Tracks By]",
"tracks":"",
"join":""
}
},
"notes":"The song titles are the names of Stockholm's districts."
}
}
当我尝试使用 node.js 运行 时,出现以下错误:
http.js:852
throw new TypeError('first argument must be a string or Buffer');
^
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.write (http.js:852:11)
at Server.<anonymous> (/home/dhiraj/http_server.js:19:44)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2109:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
at Socket.socket.ondata (http.js:1967:22)
at TCP.onread (net.js:528:27)
抱歉,如果我的问题看起来很麻烦。但是我不明白我哪里错了?
如错误消息所述,res.write
需要字符串或 Buffer 对象,因此您必须确保输出为字符串。
soll=result.country;
您在某些 callback 函数中设置了 soll
变量,与 res.write(soll);
语句相比,这些函数将在稍后的时间点执行。
所以为了调试目的你可以试试
res.write(soll?soll:'soll has null value');
我希望你会看到
soll has null value
这样至少你能明白你的情况出了什么问题:D
希望这对您有所帮助:D
当您尝试发送时,变量 soll 很可能仍然为 null。 放
console.log('soll:', soll);
就在 res.write...之前检查它。