返回未定义的获取请求

时间:2020-10-29 12:08:55

标签: javascript node.js mongodb express mongoose

我一直在做一个缩短URL的项目,结果得到一个未定义的请求。 我也得到了一个空白页,但是据我所知一切都很好,我不知道是什么错误 这是我的代码(请检查app.get部分

'use strict';
var bodyParser = require('body-parser')
var express = require('express');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var http = require("http");
var cors = require('cors');
const dns = require('dns'); 
  
var app = express();

// Basic Configuration 
var port = process.env.PORT || 3000;

/** this project needs a db !! **/ 
// mongoose.connect(process.env.DB_URI);

app.use(cors());

/** this project needs to parse POST bodies **/
// you should mount the body-parser here

app.use('/public', express.static(process.cwd() + '/public'));

app.get('/', function(req, res){
  res.sendFile(process.cwd() + '/views/index.html');
});

  
// your first API endpoint... 
app.get("/api/hello", function (req, res) {
  res.json({greeting: 'hello API'});
});
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
var saveSchema = new mongoose.Schema({
  name: String,
  url: Number,
 
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

/** 3) Create and Save a Person */
var SaveData = mongoose.model('Save', saveSchema);

//**Here's the start of my problem,i think**
app.get("/api/shorturl/:id1",function(req,res){
 SaveData.find({url:1},function(err,data){  console.log(data.name)//**i am getting undefined for this in console**
res.json(data.name);})
});
app.post("/api/shorturl/new",(req,res)=>{
var body=req.body.url;
dns.lookup(body,(err,data)=>{
  var new2= new SaveData({name:body,url:1});
new2.save((err,data)=>{res.json(new2);});
})
});

app.listen(port, function () {
  console.log('Node.js listening ...');
});

我检查了我的数据库是否输入了模式数据,它进入了数据库内部,所以检索出现了我认为的问题。

2 个答案:

答案 0 :(得分:1)

mongoose.model.prototype.find返回找到的对象的数组。如果您在某个地方的控制台中输入Array.prototype.name,则会得到undefined。而是使用mongoose.model.prototype.findOne

答案 1 :(得分:-1)

您的环境变量有效吗?我注意到您没有使用{"@timestamp":"2020-10-29T12:08:18.700Z", "log.level": "INFO", "message":"Successfully applied security_group", "service.name":"app","event.dataset":"deployment-portal-backend-navi-service.log","process.thread.name":"ForkJoinPool.commonPool-worker-19","log.logger":"com.navi.infra.portal.service.kubernetes.KubernetesManifestService"} {"@timestamp":"2020-10-29T12:08:18.700Z", "log.level": "INFO", "message":"Updating ingress", "service.name":"app","event.dataset":"deployment-portal-backend-navi-service.log","process.thread.name":"http-nio-8080-exec-1","log.logger":"com.navi.infra.portal.service.kubernetes.KubernetesManifestService","CorrelationId":"dbec0188-fd6f-458d-93e1-bc71a1b93a98"} [] io.kubernetes.client.openapi.ApiException: javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request at io.kubernetes.client.openapi.ApiClient.execute(ApiClient.java:898) at io.kubernetes.client.openapi.apis.CustomObjectsApi.getNamespacedCustomObjectWithHttpInfo(CustomObjectsApi.java:1705) at io.kubernetes.client.openapi.apis.CustomObjectsApi.getNamespacedCustomObject(CustomObjectsApi.java:1681) at com.navi.infra.portal.client.KubeClient.getNamespacedObjectStatus(KubeClient.java:44) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.getSecurityGroupIdsFromKubernetes(KubernetesManifestService.java:190) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.getSecurityGroupForDeployment(KubernetesManifestService.java:223) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.updateDeployment(KubernetesManifestService.java:149) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.lambda$updateIngressWithSecurityGroup$1(KubernetesManifestService.java:107) at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1807) at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1799) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177) Caused by: javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131) at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:311) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:267) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:258) at java.base/sun.security.ssl.SSLExtensions.<init>(SSLExtensions.java:90) at java.base/sun.security.ssl.CertificateRequest$T13CertificateRequestMessage.<init>(CertificateRequest.java:818) at java.base/sun.security.ssl.CertificateRequest$T13CertificateRequestConsumer.consume(CertificateRequest.java:922) at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:396) at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444) at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:422) at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:181) at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164) at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1460) at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1368) at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:437) at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:336) at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:300) at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:185) at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108) at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88) at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229) at okhttp3.RealCall.execute(RealCall.java:81) at io.kubernetes.client.openapi.ApiClient.execute(ApiClient.java:894) ... 14 more java.lang.NullPointerException at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.getSecurityGroupIdsFromKubernetes(KubernetesManifestService.java:199) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.getSecurityGroupForDeployment(KubernetesManifestService.java:223) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.updateDeployment(KubernetesManifestService.java:149) at com.navi.infra.portal.service.kubernetes.KubernetesManifestService.lambda$updateIngressWithSecurityGroup$1(KubernetesManifestService.java:107) at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1807) at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1799) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177) 模块或类似的东西来配置dotenv