Javascript 为什么此get方法返回此消息?
我有一个功能,可以将特定于用户的数据制成表格。它是发送查询参数userId的get方法:Javascript 为什么此get方法返回此消息?,javascript,jquery,node.js,ajax,express,Javascript,Jquery,Node.js,Ajax,Express,我有一个功能,可以将特定于用户的数据制成表格。它是发送查询参数userId的get方法: <script> let thisArray = [] let = userId = $('#userId').val(); $.ajax({ method:'GET', url:'/auth/marcacoesGET/:userId',
<script>
let thisArray = []
let = userId = $('#userId').val();
$.ajax({
method:'GET',
url:'/auth/marcacoesGET/:userId',
dataType: 'json',
data: {
userId: userId
},
success:function(marcacao){
thisArray = marcacao
buildTable(thisArray);
console.log(thisArray)
}
})
function buildTable(marcacao){
let table = document.getElementById('tabelaCliente')
for(let i = 0; i < marcacao.length; i++){
let row = `<tr>
<td>${marcacao[i].address}</td>
<td>${marcacao[i].date}</td>
<td>${marcacao[i].hour}</td>
<td>${marcacao[i].type}</td>
<td>${marcacao[i].state}</td>
</tr>`
table.innerHTML += row
}
}
</script>
这就是get方法
exports.getMarcacoes = async(req, res) => {
const { userId } = req.params;
try {
const user = await User.findById({_id : userId}).populate('marcacaoCliente');
console.log('user', user);
res.status(200).json(user.marcacaoCliente);
} catch (err) {
res.json({message:err});
}
我在浏览器控制台上收到了一条奇怪的消息,IDK,如果它是一个错误或不是:
Object { message: {…} }
message: Object { stringValue: "\"{ _id: ':userId' }\"", kind: "ObjectId", path: "_id", … }
kind: "ObjectId"
path: "_id"
reason: Object { }
stringValue: "\"{ _id: ':userId' }\""
value: Object { _id: ":userId" }
<prototype>: Object { … }
<prototype>: Object { … }
__defineGetter__: function __defineGetter__()
__defineSetter__: function __defineSetter__()
__lookupGetter__: function __lookupGetter__()
__lookupSetter__: function __lookupSetter__()
__proto__:
constructor: function Object()
hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString()
toString: function toString()
valueOf: function valueOf()
<get __proto__()>: function __proto__()
<set __proto__()>: function __proto__()
对象{消息:{…}
消息:对象{stringValue:“\”{u id:':userId'}\”,种类:“ObjectId”,路径:“\u id”,…}
种类:“ObjectId”
路径:“\u id”
原因:对象{}
stringValue:“\”{U id:':userId'}”
值:对象{u id:::userId}
:对象{…}
:对象{…}
__定义器:函数
__定义设置:函数定义设置()
__lookupGetter:函数
__lookupSetter\uuuuu:函数uuu lookupSetter\uuuuu()
__原型:
构造函数:函数对象()
hasOwnProperty:函数hasOwnProperty()
isPrototypeOf:函数isPrototypeOf()
propertyIsEnumerable:函数propertyIsEnumerable()
toLocaleString:函数toLocaleString()
toString:函数toString()
valueOf:函数valueOf()
:函数
:函数
如果您需要更多信息,请告诉,正如ciekals11在评论中所述,您应该直接将您的
userId
放入URL,而不是:userId
ajaxget请求不使用data
参数(本质上是请求的主体)。GET通常用于检索数据,而不是发送数据
编辑:使用字符串模板,您可以做到这一点
url : `/auth/marcacoesGET/${userId}`
在ajax调用中,您不应该提供用户id而不是
:userId
?例如,这个url:'/auth/marcacoesGET/:userId',
chage到这个url:'/auth/marcacoesGET/1',
实际上,你正在请求/auth/marcacoesGET/:userId?userId=1
。但是,您需要的客户端ajax URL是“/auth/marcacoesGET/”+userId
您看到的浏览器控制台输出是服务器数据库查询中的错误对象。@ciekals11确定如何使其读取当前登录用户id?您问的是如何将变量插入字符串中。。。(实际上是附加它,这更简单)另外,您应该在浏览器控制台中打开xhr
过滤器,这样您就可以仔细检查ajax URL、请求和响应了,所以:let=userId=$('#userId').val()
的工作原理令人惊讶,但应该是让userId=$('#userId').val()代码>
url : `/auth/marcacoesGET/${userId}`