- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用服务和 api 进行连接,我能够在 Catalog.component.ts 中显示 mongodb 集合中的整个数组:api.js
const express = require('express');
const router=express.Router();
const app=express();
const MongoClient=require('mongodb').MongoClient;
const ObjectID=require('mongodb').ObjectID;
var path=require('path');
var db;
const connection=(closure) => {
return MongoClient.connect('mongodb://localhost:27017', (err, client)=>{
if (err) return console.log(err);
db=client.db('angulardb');
closure(db);
});
};
const sendError =(err, res)=>{
response.status=501;
response.message=typeof err == 'object' ? err.message : err;
res.status(501).json(response);
};
let response={
status:200,
data:[],
message: null
};
router.post('/getProducts',(req, res) => {
connection((db) => {
db.collection('products')
.find()
.toArray()
.catch((err)=>{
sendError(err, res);
response.message ={ success:"Se obtuvieron los registros correctamente", error:""};
res.send({response});
})
.then((result)=>{
response.data= result;
res.send({response});
});
});
});
router.post('/getProduct',(req, res) => {
connection((db) => {
db.collection('products')
.find({id:new ObjectID(req.query.id)})
.toArray()
.catch((err)=>{
sendError(err, res);
response.message ={ success:"Se obtuvieron los registros correctamente", error:""};
res.send({response});
})
.then((result)=>{
response.data= result;
res.send({response});
});
});
});
module.exports=router;
我在服务中添加了用于目录的 getProducts 函数和用于详细信息的 getProduct 函数
mongo2.service.ts:
import { Injectable } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable()
export class Mongo2Service {
constructor( private _http: HttpClient) { }
getProducts() {
const headers = new HttpHeaders({'Content-Type': 'application/json' });
return this._http.post('/api/getProducts', { headers })
.catch( (error: any) => Observable.throw(error || 'server error'));
}
getProduct(id: number) {
const headers = new HttpHeaders({'Content-Type': 'application/json' });
const params = {'id': id};
return this._http.post('/api/getProduct', { headers, params})
.catch( (error: any) => Observable.throw(error || 'server error'));
}
}
这里我从mongodb集合catalog.component.ts获取数组:
import { Component, OnInit } from '@angular/core';
import {Mongo2Service} from '../mongo2.service';
@Component({
selector: 'app-catalog',
templateUrl: './catalog.component.html',
styleUrls: ['./catalog.component.css']
})
export class CatalogComponent implements OnInit {
products: any;
respuesta: any;
constructor( private mongo2Service: Mongo2Service) {}
ngOnInit() {
this.getProducts();
}
getProducts() {
this.mongo2Service.getProducts().subscribe(respuesta => {
this.respuesta = respuesta;
this.products = this.respuesta.response.data;
console.log(this.respuesta);
});
}
}
我显示了 mongodb 集合 collection在此列表中: list
我将一个路由器链接添加到目录组件中的该列表,并将所选元素的 id 添加到另一个名为“details”的组件,该组件在 api 和服务中具有“getProduct”方法,但 View 不显示元素的名称或编号:
import { Component, OnInit } from '@angular/core';
import {Location} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {Mongo2Service} from '../mongo2.service';
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.css']
})
export class DetailsComponent implements OnInit {
respuesta: any;
products:any;
constructor(private location: Location,
private route: ActivatedRoute,
,private mongo2Service: Mongo2Service) { }
ngOnInit() {
this.getProduct();
}
getProduct() {
const id=+ this.route.snapshot.paramMap.get('_id');
console.log('entro funcion componente');
this.mongo2Service.getProduct(id).subscribe(respuesta => {
this.respuesta = respuesta;
this.products = this.respuesta.response.data;
console.log(this.respuesta);
});
}
goBack(): void{
this.location.back();
}
}
最佳答案
我解决了这个问题,我通过将 find.() 中的 req.query._id
更改为 req.body.id
编辑了 api.js 中的 getProduct 方法,如您所见:
router.post('/getProduct',(req, res) => {
var find={ id: new ObjectID(req.body.id) };
console.log(find);
connection((db) => {
db.collection('products')
.find({_id:new ObjectID(req.body.id)})
.toArray()
.catch((err)=>{
sendError(err, res);
response.message ={ success:"Se obtuvieron los registros correctamente", error:""};
res.send({response});
})
.then((result)=>{
response.data= result;
res.send({response});
});
});
});
我还删除了 const id 处的“+”,并在数据中的位置 [0] 的详细信息方法中添加了另一个变量 (product:any)。
getProduct() {
const id = this.route.snapshot.paramMap.get('_id');
console.log(id);
this.mongo2Service.getProduct(id).subscribe(respuesta => {
this.respuesta = respuesta;
this.product = this.respuesta.response.data[0];
console.log(this.respuesta);
});
}
关于node.js - 平均堆栈 : can't display data from a single Mongodb element by it's id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50440489/
我在 MySQL 中有以下数据,我想求和(总计)然后除以行数。 例子: 我想对 AcctSessionTime 列中的所有数字求和并将其除以项目数,所以在我们的例子中 6+4+3+31=44 将它们除
我试图找出一个值在列中出现的平均次数,根据另一列对其进行分组,然后对其进行计算。 我有 3 张 table ,有点像这样 DVD ID | NAME 1 | 1 2 | 1 3
好吧,我完全被困在这里,如果这给你们带来任何不便,我深表歉意,但我需要你们的帮助。 我目前正在自学 C,并且从昨天开始慢慢地达到目标。所以我想给自己一个任务,让用户输入 3 个数字,程序必须找到这三个
我在使用 subAverage 类时遇到困难。当我使用 main 方法时,它似乎无法正常运行。基本上,subAverage 对数组中包含开始索引和结束索引的项进行平均。但是,当我运行它时,我得到了 3
像这样平均一个表不是问题 table = [[1,2,3,0],[1,2,3,0],[1,2,3,4]] 你可以 print numpy.average(table,axis=0) 但是如果我有不均匀
问题 -开发一个类平均脚本,每次运行时都会处理任意数量的结果。提示用户输入每个结果,直到他/她输入 -1。 (哨兵)确定类(class)平均值并将其写入页面。如果未输入结果(第一个输入为 -1),则显
我有 2 个包含以下数据的数组: Array1 = [A, A, A, A, B, B, B, C, C, C, C, C]; Array2 = [4, 2, 4, 6, 3, 9, 6, 5,
我有一个如下所示的文本文件: Mike 5 7 9 Terry 3 7 4 Ste 8 2 3 我写了下面的程序 从文本文件中检索数据 将文本分成由空格分隔的列 将每个名字后面的分数按顺序排序(最低在
我试图找到范围内数字的平均值(即找到 1-1000 范围内所有数字的平均值)。我编写了以下代码来执行此操作,但由于 if 语句,在运行时,代码会生成多个数字。然后我尝试使用 while-loop 代替
我有一系列事件。 1 是好的,0 是坏的。寻找寻找 1 个序列的最大、最小和平均长度的最 Pythonic 方式。 例如: seq ="00100000000000110100100000000011
我有一个包含类似于以下数据的表格: Group TimePoint Value 1 0 1 1 0 2
假设我有一个类 C,它具有属性 a。 从 Python 中的 C 列表中获取 a 总和的最佳方法是什么? 我已经尝试了以下代码,但我知道这不是正确的做法: for c in c_list: t
我有一个看起来像的数据: AAA_1 AAA_2 AAA_3 BBB_1 BBB_2 BBB_3 CCC 1 1 1 1 2 2
对于分色算法,我需要对 std::vector 中的颜色值 (QRgb) 进行平均。 您建议如何做?分别对 3 个分量求和然后取平均值?不然呢? 最佳答案 自 QRgb只是一个 ARGB 格式的 32
在this问题中,我要求对精度调用曲线进行澄清。 特别是,我问我们是否必须考虑一定数量的排名才能画出曲线,还是我们可以合理地选择自己。根据answer,第二个是正确的。 但是,现在我对平均精度(AP)
我想在 UDP 数据包丢失(或丢失)问题上获得其他 SO'ers 的经验。 最初我的理解是,给定直接点对点连接,其中网卡通过交叉电缆连接,网卡上有充足的缓冲区并及时处理所述缓冲区,“应该”没有数据包丢
我有一系列数据,这些数据是通过分子动力学模拟获得的,因此在时间上是连续的,并且在某种程度上是相关的。我可以将平均值计算为数据的平均值,我想估计与以这种方式计算的平均值相关的误差。 根据 this bo
我正在使用以下averageIf公式 =AVERAGEIF('Backend Data - Aerospace'!D:D, "Total",'Backend Data - Aerospace'!E:E
我想列出所有收入超过平均工资的员工。我对此有点迷茫。我需要将所有薪水加起来然后取平均,只显示收入高于平均水平的薪水。在这方面我需要很多帮助。 我的查询不起作用 SQL> select empno,
我正在运行一些音频压缩测试并尝试 Skype's Silk .在他们的测试应用程序中,我看到压缩率为 94%。这似乎很高,这是 Silk 的典型比率吗?这与其他音频压缩编解码器有可比性吗? 最佳答案
我是一名优秀的程序员,十分优秀!