我收到此错误
*TypeError: Cannot use 'in' operator to search for '_id' in {
at model.Document.$__buildDoc (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\mongoose\lib\document.js:210:27)
at model.Document (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\mongoose\lib\document.js:61:20)
at model.Model (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\mongoose\lib\model.js:51:12)
at new model (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\mongoose\lib\model.js:3571:13)
at Request._callback (C:\Users\seanr\projects\DerrickAlphaFebTest\routes.js:24:27)
at Request.self.callback (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\request\request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\request\request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (C:\Users\seanr\projects\DerrickAlphaFebTest\node_modules\request\request.js:1091:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)*
当我试图为我的mongodb创建一个猫鼬模型时。我正在尝试for-in循环遍历响应数据并将其保存在我的mongodb中。
模型/订单架构:
var mongoose = require('mongoose'),
bcrypt = require('bcryptjs'),
config = require('../config/database');
var Schema = mongoose.Schema;
var OrderSchema = new Schema({
orders: Object
});
var Orders = mongoose.model('Orders', OrderSchema);
module.exports = Orders;
woocommerce认证:
WooCommerceAPI = require('woocommerce-api');
var WooCommerce = new WooCommerceAPI({
url: 'https://www.example.com',
consumerKey: 'ck_example',
consumerSecret: 'cs_example',
wpAPI: true,
version: 'wc/v1',
queryStringAuth: true
})
获取请求并保存到mongoose架构:
WooCommerce.get('orders', function(err, data, res){
console.log(res);
for(var orders in res){
var O = new Orders(orders);
O.save();
}
});
res
是一个JSON对象。
另一个注意事项:由于某种原因,它仍然保存到我的数据库中,但它几乎就像是在无限循环中,因为它比我预期的节省了更多的数据。我没有使用for-in循环的经验,但认为这是解决这个问题的最佳方法。如果您对我出错的地方有任何见解,我将非常感激!
答案 0 :(得分:0)
我通过将模型的规范添加到我的获取请求来修复此问题。
WooCommerce.get('orders', function(err, data, res){
console.log(res);
for(var orders in res){
var O = new Orders({orders: wooorders});
O.save();
}
});