NodeJS DB查询

时间:2019-10-25 12:46:41

标签: node.js express

在以下代码中:

app.use('/Catalog', function(req, res)
  {                                                                                                             
        let query = "select id, name from Category where id = ?";
        let queryError = "select * from Category";
        db.all(query, [req.query.id], (err, rows) => {
                if(err == null){
                        res.writeHead(200, { 'Content-Type': 'text/plain'});
                        res.write(JSON.stringify(rows));
                        res.end();
                }
                else{
                        console.log(err);
                        db.all(queryError, (err, rows) => {
                                res.writeHead(200, { 'Content-Type': 'text/plain'});
                                res.write(JSON.stringify(rows));
                                res.end();
                        });
                }
        });

  });

似乎else条件不起作用。 这是我的Category表中的行:

1|Motorcycles
2|Classic Cars
3|Trucks and Buses
4|Vintage Cars
5|Planes
6|Ships
7|Trains

所需的行为是,如果我请求的ID不在表中(例如/catalog?id=8),则服务器应返回所有可用的类别。 但是在这里,它不返回所有类别,而是返回

[]

1 个答案:

答案 0 :(得分:1)

您正在测试是否存在错误

数据库成功 返回所有个匹配行。

错误是诸如“与数据库的网络连接失败”,“请求不存在的列”或SQL中的语法错误之类的东西。

测试Private Sub cmdsearch_click() Dim rowResult As Long Dim strSearch As String rowResult = 2 strSearch = txtkeywords.Value Dim rowLast As Long rowLast = Worksheets("Stock Data").Cells(Rows.Count, 1).End(xlUp).Row Dim rngFound As Range Dim firstAdress As String With Worksheets("Stock Data").Range("A1:A" & rowLast) Set rngFound = .Find(strSearch, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) If Not rngFound Is Nothing Then firstAddress = rngFound.Address Do Worksheets("Sheet1").Cells(rowResult , 1).Value2 = rngFound.Value2 Worksheets("Sheet1").Cells(rowResult , 2).Value2 = rngFound.Offset(0, 1).Value2 Worksheets("Sheet1").Cells(rowResult , 3).Value2 = rngFound.Offset(0, 2).Value2 rowResult = rowResult + 1 Set rngFound = .FindNext Loop While rngFound.Address <> firstAdress Else MsgBox "Sorry No products found, please request a price" End If End With Lstsearchresults.RowSource = "SearchResults" End Sub 是否为rows.length(假设0是一个数组)。