嗯,我已经开发了一个web服务app.js,它可以处理mongodb数据库中的一些产品,邮递员似乎一切正常,我可以做完整的CRUD ( GET,POST,UPDATE,DELETE)和我的vb.net应用程序,我可以做GET,但POST无论如何都不会工作,这里需要一些帮助。
这是我的模型
var mongoose = require('mongoose');
//Plato Schema
var platoSchema = mongoose.Schema({
nombre_plato:{
type: String,
required: true
},
categoria_plato:{
type: String,
},
sub_categoria_plato:{
type: String,
},
descripcion:{
type: String,
},
fecha_creacion:{
type: Date,
default: Date.now
},
precio_plato:{
type: Number,
},
foto_plato:{
type: String,
},
estado:{
type: String,
},
estado_contorno:{
type: String,
},
contorno:{
type: String,
},
});
var Plato = module.exports = mongoose.model('Plato', platoSchema);
module.exports.addPlato = function(plato, callback){
Plato.create(plato, callback);
}这是我的服务器模块
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
app.use(bodyParser.json());
Plato = require('./models/plato');
//Connect to mongoose
mongoose.connect('mongodb://MYLANIP:PORT/DATABASE');
var db = mongoose.connection;
//metodo POST
app.post('/api/platos', function(req, res){
var plato = req.body;
Plato.addPlato(plato, function(err, plato){
if(err){
throw err;
}
res.json(plato);
});
});
app.listen(3000);
console.log('Running on port 3000');下面是vb.net POST方法
Dim HttpWebRequest As WebRequest = WebRequest.Create("http://MYSERVERADDRESS/api/platos")
ServicePointManager.Expect100Continue = False
HttpWebRequest.ContentType = "application/json"
Dim encoding As New UTF8Encoding
Dim bytejson As Byte() = encoding.GetBytes(jsonString)
HttpWebRequest.Method = "POST"
Dim StreamWriter As Stream = HttpWebRequest.GetRequestStream
StreamWriter.Write(bytejson, 0, bytejson.Length)
Dim httpResponse As HttpWebResponse = HttpWebRequest.GetResponse()
Dim streamReader = New StreamReader(httpResponse.GetResponseStream())
RichTextBox1.Text = streamReader.ReadToEnd()作为服务器的响应,vb.net向我显示错误407,服务器崩溃,并显示events.js:160验证错误:柏拉图验证失败。
我知道当服务器试图从vb.net获取数据,然后崩溃时,会出现一些问题,但是
如果我从chrome邮递员那里做GET POST DELETE UPDATE,我可以像我说的那样完美地工作,我也可以从vb.net应用程序中get,但我不能做POST,我已经尝试了所有的方法,但它拒绝工作。我必须说,如果我用像https://jsonplaceholder.typicode.com/posts这样的测试服务器来测试vb.net服务器,它也能完美地工作,所以结论是:
邮递员->发布我的server.js都很好我的vb.net应用程序POST->Json测试服务器都很好
但我的应用程序vb.net POST -> my server.js nothing。
有什么想法吗?提前感谢
发布于 2017-01-18 09:55:09
嗯,看起来我想办法解决了..所有的问题都是我所不知道的,比如
var platoSchema = mongoose.Schema({
nombre_plato:{
type: String,
required: true
},
categoria_plato:{
type: String,
},
sub_categoria_plato:{
type: String,
},
descripcion:{
type: String,这是我的mongoose模式的一部分,我在vb.net中的类是
Private _nombre_plato As String
Private _categoria_plato As String
Private _sub_categoria_plato As String
Private _descripcion As String
Private _fecha_creacion As Date
Private _precio_plato As Double
Private _foto_plato As String
Private _estado As String
Private _estado_contorno As String
Private _contorno As String我的错误或缺乏知识是,当我重构时,自动生成了
Public Property Nombre_plato As String
Get
Return _nombre_plato
End Get
Set(value As String)
_nombre_plato = value
End Set
End Property
Public Property Categoria_plato As String
Get
Return _categoria_plato
End Get
Set(value As String)
_categoria_plato = value
End Set
End Property
Public Property Sub_categoria_plato As String
Get
Return _sub_categoria_plato
End Get
Set(value As String)
_sub_categoria_plato = value
End Set
End Property
etc
etc
etc正如您所看到的,访问由vb.net自动生成的类的属性,将我的小写名称更改为相同的名称,但首字母大写,因此它与mongoose架构不匹配。伙计,我都快疯了.
好吧,希望这也能帮助到其他人。
https://stackoverflow.com/questions/41706209
复制相似问题