使用Mongoose将文档(记录)插入MongoDB有哪些不同的方法?
我目前的尝试:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var notificationsSchema = mongoose.Schema({
"datetime" : {
type: Date,
default: Date.now
},
"ownerId":{
type:String
},
"customerId" : {
type:String
},
"title" : {
type:String
},
"message" : {
type:String
}
});
var notifications = module.exports = mongoose.model('notifications', notificationsSchema);
module.exports.saveNotification = function(notificationObj, callback){
//notifications.insert(notificationObj); won't work
//notifications.save(notificationObj); won't work
notifications.create(notificationObj); //work but created duplicated document
}
Run Code Online (Sandbox Code Playgroud)
知道为什么插入和保存在我的情况下不起作用?我尝试创建,它插入2个文件而不是1.这很奇怪.
我有一条路线如下:
var express = require('express');
var router = express.Router();
var request = require('request');
router.get('/', function(req, res, next) {
request({
uri: 'http://www.giantbomb.com/api/search',
qs: {
api_key: '123456',
query: 'World of Warcraft: Legion'
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body)
}
}
});
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
我正在尝试对Giant Bomb API进行API调用,以恢复有关魔兽世界的任何数据.
问题是,路线刚加载; 它没有做任何事情或它没有超时,它只是连续加载.
我不知道我做错了什么,但话虽如此......我也不知道什么是对的.我一直在努力学习.
任何帮助都会很棒.
谢谢