小编Dum*_*uma的帖子

Mongodb $其中查询始终与nodejs一致

当我使用在nodejs中的"$ where"子句中传递的函数查询我的数据库时,它总是返回db中的所有文档.

例如,如果我这样做

var stream = timetables.find({$where: function() { return false; }}).stream();
Run Code Online (Sandbox Code Playgroud)

它将所有文件归还给我.相反,如果我这样做

var stream = timetables.find({$where: 'function() { return false; }'}).stream();
Run Code Online (Sandbox Code Playgroud)

该函数实际执行,此代码不返回任何文档.

问题是,如果我在字符串my function中转换上下文的绑定被删除,我需要它们来进行更复杂的查询.例如:

var n = 1;
var f = function() { return this.number == n; }
var stream = timetables.find({$where: f.toString()}).stream();
// error: n is not defined
Run Code Online (Sandbox Code Playgroud)

这是正常行为吗?我怎样才能解决我的问题?请原谅我英语不好!

mongodb node.js

6
推荐指数
1
解决办法
8190
查看次数

useState conflicts with async operations in React

In a React functional component with the useState hook I don't know how to run multiple async operations without having conflict on the final result.

If the initial state is an object and every async operation performs a change on a given key, it's possible to use the spread operator in order to set a new state. Example:

const [oldState, setNewState] = React.useState({ /* something */ });
const asyncOperation = async () => {
   await somethingAsync();
   setNewState({
      ...oldState,
      key: 'newValue', …
Run Code Online (Sandbox Code Playgroud)

javascript async-await reactjs react-hooks

6
推荐指数
1
解决办法
128
查看次数