gpt4 book ai didi

javascript - meteor minimongo 得到不一致的 collection.findOne() 结果

转载 作者:行者123 更新时间:2023-12-01 17:56:56 26 4
gpt4 key购买 nike

几个小时以来,我一直在尝试调试一大块代码,用头撞墙,最后将我的问题精确定位到代码中的某个位置,该位置将 collection.findOne() 调用的结果分配给一个变量给我的数据与我在前一行的同一 findOne() 的 console.log() 中看到的数据不同。

prePostState = function(thisStID) {  
console.log(Students.findOne({_id:thisStID}));
var stTemp = Students.findOne({_id:thisStID});
console.log(stTmp);
var testsTemp = stTmp.tests;

集合对象有一个“测试”数组。在本例中,数组包含 3 个对象作为其元素。

虽然两个 console.log() 行都返回类似这样的内容

 Object {_id: "eXf9dqQbaemKS24Ti", name: "Student,Name", group: "none", site: "SiteName", tests: Array[3]}  

展开每个显示不同的数据。第一个显示正确的测试:Array[3],第二个显示测试:Array[1],并且该数组中的单个元素也包含与完整数组中的匹配元素不同的数据。

----更新----
做一些进一步的测试,我稍微改变了代码。

prePostState = function(thisStID) {
console.log(Students.find({_id:thisStID}).fetch()); //1
var stTmp = Students.find({_id:thisStID}).fetch();
console.log(stTmp); //2
console.log(stTmp[0].tests.length); //3
for(var i = 0; i < stTmp[0].tests.length; i++) {
console.log(stTmp[0].tests[i]); //4
}

1 返回:

[Object]
0: Object
_id: "AqLHB8hT8GxzQ7zyD"
group: "none"
name: "Student,Name"
site: "SiteName"
tests: Array[3]

2 返回:

[Object]
0: Object
_id: "AqLHB8hT8GxzQ7zyD"
group: "none"
name: "Student,Name"
site: "SiteName"
tests: Array[1]

3 返回:

3  

4 处的 for 循环重复三次并打印出 tests 数组中的三个对象中的每一个。

显然,这意味着我可以访问我需要的数据。而不是

var testArray = stTmp.tests;

这给我留下了一个只有一个元素的数组,我只需要获取 stTmp.tests 的长度,然后使用 for 循环按索引访问每个元素并将它们插入到 testArray 变量中。

所以我可以继续,但我仍然不明白我所看到的行为。在这一点上,我有一段时间要继续取得进展,但当我有时间时,我可能会重新审视它,并尝试在 meteor 垫或其他我可以与之共享完整代码的形式中复制它。

最佳答案

1) 如果您修改了 Minimongo 的返回值,不要指望它会持续存在。 Minimongo 就是专门这样写的,所以你不得不使用 update 运算符来更新值。

2) 正确的投影 API 是 Coll.find({..selector..}, {fields:{..projection..}})

关于javascript - meteor minimongo 得到不一致的 collection.findOne() 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com