node.js - 如何解决 mongodb mongoose 中的“TypeError: db.find(...).toArray is not a function”?
问题描述
我正在尝试查询我的 mongodb 集合以更新字段,但在尝试使用.toArray
循环所有文档时遇到错误。
每当我运行下面的代码时,它都会显示TypeError: meme.find(...).toArray is not a function
.
我做错了什么,我能做些什么来解决它?
var mongoose = require('mongoose');
const mongoolia = require('mongoolia').default;
var algoliasearch = require('algoliasearch');
//meme schema
var meme = require('../app/model/meme');
const TagSchema = new mongoose.Schema({
tagarray: { type: String, required: true, algoliaIndex: true },
});
TagSchema.plugin(mongoolia, {
appId: 'QQVL4GOPOL',
apiKey: 'd93766f242500de7a13183eeeb6a7934',
indexName: 'test1'
})
meme.find().toArray(function(memes){
meme.update({_id: memes._id}, {$set: { objectID: memes._id.toString().slice(10).slice(0,24)}});
});
解决方案
更改find().toArray()
为find().fetch();
推荐阅读
- json - 如何在自定义 VS Code tmLanguage 文件中设置多个匹配项?
- git - GIT:如何将文件添加到 repo 但忽略它以进行更改
- c# - UIWebview 中的 SVG 图像未正确缩放
- javascript - 简化 JS 中嵌套的父子代码
- java - 用随机列表中的 2 个值填充 2D 数组(2048 游戏)
- php - 是否存在 $_POST 不是数组的情况?
- google-my-business-api - Google My Business API 使用过滤器参数不清楚
- android - Firebase - 不同的包名称和应用 ID
- azure - Azure - 在特定时间高效运行代码,而不是按计划运行
- android - 如何修复此布局?