使用angularjs和节点。带有Cloudant DB搜索API的后端。

时间:2022-04-22 16:24:40

I am trying to search my cloudant DB from my node.js back end using the search API, then display what is returned on a webpage.

我正在尝试从我的节点搜索我的cloud DB。后端使用搜索API,然后显示网页上返回的内容。

I am using an angular MVC and I am a bit new to it, so sorry if this is a bit simple.

我使用的是一个角MVC,我对它有点陌生,抱歉,如果这有点简单的话。

I have a html front end which contains this -

我有一个html前端包含这个-

<button class="btn btn-default" ng-click="getDataandHeadings()"></button>

<table>
<tr ng-repeat="doc in myData">
  <td>{{doc.doc.TerritoryTypeName}}</td>
  <td>{{doc.doc.TeamOwnerSerialNo}}</td>
  <td>{{doc.doc.TeamOwnerName}}</td>
  <td>{{doc.doc.TeamOwnerNotesID}}</td>
  <td>{{doc.doc.SellerSerialNo}}</td>
  <td>{{doc.doc.SellerName}}</td>
  <td>{{doc.doc.SellerNotesID}}</td>
</tr>
</table>

A controller -

一个控制器,

'use strict';

var app = angular.module('app', false);

app.controller('greetingscontroller', function($scope, $http) {

$scope.getDataandHeadings = function() {

     $http.get('/retrieve').success(function(data) {

       $scope.myData = data;
     }).error(function(err) {
        //handle the error
     });
  };

});

Back end Node application -

后端节点应用程序-

var express = require('express');
var secrets = require('./secrets.js');
var Cloudant = require('cloudant');
var csv = require('csv-parser');
var fs = require('fs');
var cloudant = Cloudant({account: secrets.cloudantUser, password:secrets.cloudantPassword});
var cfenv = require('cfenv');


var app = express();
app.use(express.static(__dirname + '/public'));

var arr = [];
var headings = [];
var bufferString;
var i;
var db = cloudant.db.use('my_sample_db');


app.get('/retrieve', function(req, res) {

var db = cloudant.db.use('my_sample_db');


// db.get('_all_docs', {include_docs: true}, function(err, body) {
//    if(!err) {
//      console.log(body.rows.length +'  '+body.total_rows);
//      for (var i = 0; i < body.rows.length; i++) {
//         console.log('Document id: %s', body.rows[i].id);
//      }
//
//      res.status(200).json(body.rows);
//    }
//    if(err) {
//      console.log(err);
//    }
//  });

db.search('app', 'mySearch', {q:'TerritoryTypeName:NHS'}, function(err, body) {

console.log(body.rows.length +'  '+body.total_rows);
for (var i = 0; i < body.rows.length; i++) {
   console.log('Document id: %s', body.rows[i].id);
}
res.status(200).json(body.rows);

 if (err) {
  console.log(err);
 }
});

});



var appEnv = cfenv.getAppEnv();

app.listen(appEnv.port, '0.0.0.0', function() {

console.log("server starting on " + appEnv.url);
});

As you can see, in my back end I am making 2 similar API calls to the DB - db.get and db.search - (one is commented out). The one the is commented out simply returns everything that is in the database, and the second one does a query using my key:value pair - TerritoryTypeName:NHS.

如您所见,在我的后端,我正在对DB - DB进行两个类似的API调用。得到和db。搜索-(一个被注释掉)。注释掉的那个只返回数据库中的所有内容,第二个则使用我的key:value - TerritoryTypeName:NHS进行查询。

The commented out call works how I want it to, and when I click the button on the front end, the data in the DB is displayed.

注释掉的调用按照我希望的方式工作,当我单击前端的按钮时,DB中的数据将显示出来。

The other API call seems to not be working, as nothing is displayed on the front end when I click. I don't understand why one of them works and the other doesn't when they are so similar?

另一个API调用似乎不起作用,因为当我单击时,前端没有显示任何内容。我不明白为什么他们中的一个可以工作,而另一个却不能,当他们如此相似的时候?

However, I think the db.search function is working correctly, as the console.log line works well and displays onto the terminal the expected returned entries from the DB based on the query. I think the problem may lie either in the html, or the Controller? If anyone can lend any suggestions that would be great! Here is a copy of the terminal:

但是,我认为是db。作为控制台,搜索功能正在正常工作。日志行工作得很好,并根据查询在终端上显示来自DB的预期返回条目。我认为问题可能在于html或者控制器?如果有人能提供任何建议那就太棒了!这是终端机的副本:

server starting on http://localhost:6001
2  2
Document id: 4092be64a755bc04432a5187cdf70006
Document id: 4092be64a755bc04432a5187cdf6f9c2
{ total_rows: 2,
  bookmark: 'g1AAAACqeJzLYWBgYMpgTmHQSUlKzi9KdUhJMtFLyilNzc2s0C3N1i0uScxLSSxKMdRLzskvTUnMK9HLSy3JAenKYwGSDAuA1P____dnZTC52R9eXAUSS2RANdGcJBMfQEz8DzbxWc1JsImMWQAEtDc6',
  rows: 
   [ { id: '4092be64a755bc04432a5187cdf70006',
       order: [Object],
       fields: [Object] },
     { id: '4092be64a755bc04432a5187cdf6f9c2',
       order: [Object],
       fields: [Object] } ] }

1 个解决方案

#1


2  

Try adding include_docs to your search query:

尝试在搜索查询中添加include_docs:

{q:'TerritoryTypeName:NHS', include_docs:true}

It looks like the data you are returning does not include the doc objects, which you are trying to access in your HTML:

看起来您要返回的数据不包括文档对象,您试图在HTML中访问这些对象:

<tr ng-repeat="doc in myData">
  <td>{{doc.doc.TerritoryTypeName}}</td>
  <td>{{doc.doc.TeamOwnerSerialNo}}</td>
  ...
</tr>

It looks like you are expecting the data returned from the server to look like this:

看起来您期望从服务器返回的数据是这样的:

rows : [
  {
    doc : {
      TerritoryTypeName: "xxx",
      TeamOwnerSerialNo: "xxx",
      ...
  },
  {
    doc : {
      TerritoryTypeName: "yyy",
      TeamOwnerSerialNo: "yyy",
      ...
  }
]

However, it is being returned from the server like this:

但是,它是从服务器返回的,如下所示:

rows: [
  {
    id: '4092be64a755bc04432a5187cdf70006',
    order: [Object],
    fields: [Object]
  },
  {
    id: '4092be64a755bc04432a5187cdf6f9c2',
    order: [Object],
    fields: [Object]
  }
]

#1


2  

Try adding include_docs to your search query:

尝试在搜索查询中添加include_docs:

{q:'TerritoryTypeName:NHS', include_docs:true}

It looks like the data you are returning does not include the doc objects, which you are trying to access in your HTML:

看起来您要返回的数据不包括文档对象,您试图在HTML中访问这些对象:

<tr ng-repeat="doc in myData">
  <td>{{doc.doc.TerritoryTypeName}}</td>
  <td>{{doc.doc.TeamOwnerSerialNo}}</td>
  ...
</tr>

It looks like you are expecting the data returned from the server to look like this:

看起来您期望从服务器返回的数据是这样的:

rows : [
  {
    doc : {
      TerritoryTypeName: "xxx",
      TeamOwnerSerialNo: "xxx",
      ...
  },
  {
    doc : {
      TerritoryTypeName: "yyy",
      TeamOwnerSerialNo: "yyy",
      ...
  }
]

However, it is being returned from the server like this:

但是,它是从服务器返回的,如下所示:

rows: [
  {
    id: '4092be64a755bc04432a5187cdf70006',
    order: [Object],
    fields: [Object]
  },
  {
    id: '4092be64a755bc04432a5187cdf6f9c2',
    order: [Object],
    fields: [Object]
  }
]